What's the difference declaring multiple empty variables
Actually when you do
var objectA = {};
var objectB = {};
or
var objectA = new Object();
var objectB = new Object();
You are creating two different JavaScript objects and they are passed to objectA
and objectB
. But when you do
var objectA = objectB = {};
or
var objectA = objectB = new Object();
In fact, you are creating only one object , and the two objectA
and objectB
refer to the same object.
You can confirm this by checking if both objects are the same or not, for example
var objectA = {}, objectB = {};
console.log(objectA === objectB);
// false
var objectC = objectD = {};
console.log(objectC === objectD);
// true
Note:
var objectC = objectD = {};
will be evaluated as follows
var objectC = (objectD = {});
That's why the two objectC
and objectD
refer to the same object.
Important: As dfsq mentions in a comment , the last <global scope> example will leak objectD
. So don't use this pattern.
source to share
The first approach creates an object and assigns a variable reference to it. Then it creates another object and assigns a reference to it to another variable.
The second approach creates an object and assigns a variable reference to it. Then it assigns another reference to the same object to another variable.
source to share
It is the same; {}
creates a new one Object
:
var a = {};
var b = new Object();
If you assign the same object instance, empty or otherwise, to two variables, they will refer to the same instance.
var b;
var a = b = {}
a.Foo = 'foo';
// "foo"
alert(b.Foo);
If you create a different instance for each, they will refer to different instances:
var b = {};
var a = {};
a.Foo = 'foo';
// ""
alert(b.Foo);
If you are doing one of these chaining assignments with a value type such as integer, it will be in a different implementation-dependent way, but since integers have no properties you can change, this is not a problem. I spent some time with the C source for the old Netscape <4 JavaScript engine back when they first released the source. The integers were stored in the pointer itself, shifted left by one or two bits, and labeled in the least significant bits (the allocated memory was aligned at four-byte boundaries, so those two bits were always zero on a valid pointer). IIRC Gnu Common Lisp does this too.
source to share
First case
If you say it
var objectA = {};
var objectB = {};
or
var objectA = new Object();
var objectB = new Object();
You are creating two different objects. If you compare both objects
objectA == objectB //return false
Second case
When you speak
var objectA = objectB = {};
or
var objectA = objectB = new Object();
here objectA
and objectB
both refer to the same object. Because first you create an object and assign it objectA
and objectB
therefore both will have a reference to the created object.
In this condition
objectA == objectB //return true
Note: -
If you want to create two objects with the same reference, do it in two lines, like below
var objectA = new Object();
var objectB = objectA;
Because you shouldn't uselessly use your global scope.
source to share