Reputation: 15819
Let's say I have the following object:
var lol = {
section: {},
other: {foo: 'bar', foob: 'baz'}
};
Now if I do the following:
lol.section.other = lol.other;
will a reference be made linking section.other
to other
or will the entire other
object be copied and placed in section
?
Upvotes: 0
Views: 168
Reputation: 707218
As SLaks said, javascript assigns objects as a reference (without copying). It's easy to test or see yourself:
var lol = {
section: {},
other: {foo: 'bar', foob: 'baz'}
};
lol.section.other = lol.other;
lol.other.foo = 'test';
console.log(lol.section.other.foo); // will be 'test', not 'bar'
You can see it here: http://jsfiddle.net/jfriend00/r73LH/.
Upvotes: 1
Reputation: 5054
Like quite a few other OO languages, JavaScript also passes and assigns the object by reference therefore, you are only creating a new reference to an existing object.
Where JavaScript breaks away from other OO languages is in inheritance and in encapsulation. So be cautious in those areas.
Upvotes: 2
Reputation: 887305
You're creating two references to the same object.
Javascript objects are never implicitly copied.
Upvotes: 8