/    Sign up×
Community /Pin to ProfileBookmark

Objects passed by reference

I’ve been running into a problem with a script that I’m writing. I’m trying to duplicate an object by assigning it to another variable, but it seems to be passing it by reference rather than assignment. Any changes the to new variable change the old variable as well and vice-versa. Here is a short script to illustrate what is going on:

[CODE]function obj() {
this.variable = true;
}
x = new obj();
y = x;
alert(x.variable);
y.variable = false;
alert(x.variable);[/CODE]

It will alert “true” and then alert “false” showing that x has changed even though I’ve only changed y. I want to be able to change them separately.

Does anyone know what’s going on and how to solve this problem?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KorFeb 21.2007 — Maybe:
<i>
</i>function obj(bool) {
this.variable = bool;
}
x = new obj(true);
y = new obj(false)
Copy linkTweet thisAlerts:
@ncantorauthorFeb 21.2007 — Thanks, but the function I'm actually using in the script is much more complicated than this simple one I've posted. The variables that I pass to create the object get destroyed or modified before I need to duplicate the object, so I can't create both at the same time. I'm writing a script to make a schedule from a list of classes. I'm making it so that X and Y are arrays of class objects with their name, times, etc.. X is a reference which contains all the classes and Y will contain just the classes the user selects. I need to be able to re-copy objects from X into Y in case the user changes the classes he has selected. However, the X classes are modified by modifying Y classes. The variables I passed to the constructor function have already been overwritten so I cannot just pass them a second time to create Y. It's more like:

[CODE]
function obj(bool) {
this.variable = bool;
}
var X = new Array();

function loadXML() {
//loading and parsing function
return new Array(true, false, true) //In reality, not defined explicitly, but variables parsed from XML
}
var input = parseXML(xml);

for (i in input)
X.push(new obj(input[i]));

var Y = new Array(X[0], X[2]);

alert(X[0].variable);
Y[0].variable = false;
alert(X[0].variable);
alert(X[2].variable);
Y[1].variable = false;
alert(X[2].variable);
[/CODE]
Copy linkTweet thisAlerts:
@mrhooFeb 21.2007 — You need a second object:

[CODE]function copyObj(obj){
var O=new obj.constructor();
for(var p in obj)O[p]=obj[p];
return O;
}[/CODE]
Copy linkTweet thisAlerts:
@ncantorauthorFeb 21.2007 — You're amazing, that works like a dream!

Thanks a lot!
×

Success!

Help @ncantor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...