Reply
Ob1=Ob2 - now changes to Ob1 affect Ob2?
Old 01-05-2009, 12:57 PM Ob1=Ob2 - now changes to Ob1 affect Ob2?
hiptobesquare's Avatar
Extreme Talker

Posts: 182
Location: London UK
Trades: 0
hello

im having an issue with a couple of javascript objects which are behaving quite strangely, could someone tell me if the following is standard behaviour, and if so, how can i change it.

i have 2 Objects in my code. when a user makes changes to a form a function is called which stores all of the current form selections into Ob1, then the form selections are validated and IF the new selection is permitted and validation passes, i make Ob1=Ob2;. Otherwise Ob2 remains as it was before the function was called.

Following this function i reload all of the form selections according to the info stored in Ob2.

The problem is that as soon as i make Ob1=Ob2, subsequent changes to Ob1 affect Ob2 automatically. Is this what should be happening? If so, is there a way i can stop it?

thanks

mark m
hiptobesquare is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 01-05-2009, 02:02 PM Re: Ob1=Ob2 - now changes to Ob1 affect Ob2?
wayfarer07's Avatar
NYE-KEE

Posts: 3,136
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Objects that are created from the DOM will automatically update their properties when the DOM is updated, so you would have to copy the attributes by creating a new object with properties that represent each form feature if you don't want this to happen.
__________________
Wayfarer | jQuery Tooltip Plugin
Freelance Jobs Available
If Google is the Coca-Cola of Web search, Bing is RC Cola
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 01-05-2009, 04:15 PM Re: Ob1=Ob2 - now changes to Ob1 affect Ob2?
chrishirst's Avatar
Super Moderator

Posts: 21,650
Location: Blackpool. UK
Trades: 0
Setting objB = objA only creates a copy of objA. It does not create a new instance of the object.
What you need to do is to create a new object from objA then they become separate entities.

var objB = new objA

objB then inherits all the properties and values from objA, but only at the time of instantiation, so subsequent changes to the objA will not affect objB.

So. Welcome to the wonderful world of Object Oriented Programming and Inheritance then
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Growing old is mandatory - Growing up is optional
Code Samples | People Counting System | Bits & Bobs
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-05-2009, 04:53 PM Re: Ob1=Ob2 - now changes to Ob1 affect Ob2?
hiptobesquare's Avatar
Extreme Talker

Posts: 182
Location: London UK
Trades: 0
massive thanks to you both.

im sure there's a very good reason for this inheritance you speak of, but at the moment to me it seems like odd behaviour since all other things ive encountered in programming have worked differently.

thanks again for enlightening me.
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 01-05-2009, 10:20 PM Re: Ob1=Ob2 - now changes to Ob1 affect Ob2?
wayfarer07's Avatar
NYE-KEE

Posts: 3,136
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
If you really want to be confused, study the prototype property (which exists on every single JavaScript object, which is pretty much everything since everything in JavaScript is an object). Inheritance, in JavaScript, is said to be prototypal (as opposed to classical). It is the only mainstream language that uses this type of inheritance.
__________________
Wayfarer | jQuery Tooltip Plugin
Freelance Jobs Available
If Google is the Coca-Cola of Web search, Bing is RC Cola
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 01-06-2009, 03:25 PM Re: Ob1=Ob2 - now changes to Ob1 affect Ob2?
hiptobesquare's Avatar
Extreme Talker

Posts: 182
Location: London UK
Trades: 0
ive been looking at a few object tutorials and prototype has come up quite a bit, i dont understand a word of it at the moment, the only reason i began using objects as opposed to simple arrays was that JSON seemed to prefer them. Im extremely confused
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 01-07-2009, 02:11 AM Re: Ob1=Ob2 - now changes to Ob1 affect Ob2?
wayfarer07's Avatar
NYE-KEE

Posts: 3,136
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
technically, an associative array is an object. Arrays and objects can both be expressed like this:
Code:
var myArray = [];
myArray["myKey"] = "Hello World!";
var myObject = {};
myObject.myProperty = "Hello World!";

alert(myArray["myKey"]);
alert(myObject["myProperty"]);
both will alert "Hello World!". Note that I alerted the myObject.myProperty as if it were an array. This is perfectly valid, though it is not often used.
__________________
Wayfarer | jQuery Tooltip Plugin
Freelance Jobs Available
If Google is the Coca-Cola of Web search, Bing is RC Cola
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 01-08-2009, 11:31 AM Re: Ob1=Ob2 - now changes to Ob1 affect Ob2?
hiptobesquare's Avatar
Extreme Talker

Posts: 182
Location: London UK
Trades: 0
yeah i get the basics now, its when people start mentioning classes and things that i lose track. Ive not yet come into a situation where i havnt been able to do what i need with the basic knowledge i have, and therefore have not begun to confuse myself with proper programming practises. my code is cumbersome and probably doesnt follow the standards, but i usually get it to work as i want and you cant teach an old dog new tricks.
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Ob1=Ob2 - now changes to Ob1 affect Ob2?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.14341 seconds with 13 queries