John Yin
John Yin

Reputation: 8367

what's difference multiple blank variables declare

literal object methods:

var objectA = {}; 
var objectB = {};

vs

var objectA = objectB = {};

constructor object methods:

var objectA = new Object();
var objectB = new Object();

vs

var objectA = objectB = new Object();

Upvotes: 3

Views: 49

Answers (5)

Mritunjay
Mritunjay

Reputation: 25892

First case

When you say this

var objectA = {}; 
var objectB = {};

or

var objectA = new Object();
var objectB = new Object();

You are creating two different objects. If you will compare both objects

objectA == objectB //return false

Second Case

When you say

var objectA = objectB = {};

or

var objectA = objectB = new Object();

here objectA and objectB both refer to same object. Because first you are creating an object and assigning that to objectA and objectB so both will have the reference of created object.

In this condition

objectA == objectB //return true

Note:-

If you want to create two objects with same reference, do it in two lines like bellow

var objectA = new Object();
var objectB = objectA;

Because you shouldn't mess your global scope unnecessarily.

Upvotes: 0

thefourtheye
thefourtheye

Reputation: 239573

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 referred by objectA and objectB. But when you do

var objectA = objectB = {};

or

var objectA = objectB = new Object();

You are actually creating only one object and making both objectA and objectB refer the same object.

You can confirm this by checking if both the objects are one and the same or not, like this

var objectA = {}, objectB = {};
console.log(objectA === objectB);
// false
var objectC = objectD = {};
console.log(objectC === objectD);
// true

Note:

var objectC = objectD = {};

will be evaluated like this

var objectC = (objectD = {});

That is why both objectC and objectD refer the same object.

Important: As dfsq mentions in the comment, in the last example, objectD will be leaked to the global scope. So, avoid using this pattern.

Upvotes: 1

These are the same; {} creates a new Object:

var a = {};
var b = new Object();

If you assign the same instance of an object, empty or otherwise, to two variables, they will both 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 do one of those chained assignments with a value type like an integer, that'll be different, in implementation dependent ways, but since integers have no properties you can alter, it's a non-issue. I spent some time with the C source for the old Netscape < 4 JavaScript engine back when they first released the source. Integers were stored in the pointer itself, left-shifted by one or two bits and tagged in the lowest order bits (allocated memory was aligned on four-byte boundaries so those two bits were always zero on a valid pointer). IIRC Gnu Common Lisp does it that way too.

Upvotes: 0

Amit Joki
Amit Joki

Reputation: 59272

As far as new Object() and {} are concerned, both result in an Object instance.

As far as var objA = objB = {} is concerned, both objA and objB share the same reference to the Object instance but that is not the case when you assign them separately.

Upvotes: 0

Quentin
Quentin

Reputation: 944016

The first approach creates an object and assigns a reference to it to a variable. Then it creates another object and assigns a reference to that to a different variable.

The second approach creates an object and assigns a reference to it to a variable. Then it assigns another reference to the same object to a different variable.

Upvotes: 0

Related Questions