user2594803
user2594803

Reputation:

Object and array with self referencing

I am very interested in understanding JavaScript behavior.

Object :

object = {object : window.object};
object = {object : window.object};

console.log(object === object); // true
console.log(object.object === object); // false
console.log(object.object === object.object); // true
console.log(object.object.object === object.object); // false

Array :

array = [window.array];
array = [window.array];

console.log(array === array); // true
console.log(array[0] === array); // false
console.log(array[0] === array[0]) // true
console.log(array[0][0] === array[0]) // false

Why

object.object.object === object.object

returns false ???

Upvotes: 2

Views: 1793

Answers (3)

Anoop
Anoop

Reputation: 23208

Let explain it line by line

    object = {object : window.object};
    //here object.object is undefined and object new object so object !== object.object
    //lets obj1 = {object: undefined}

    object = {object : window.object};
    //here object is new instance of Object while object.object is equal to older object
   //basically object != object.object 
    // obj2 = {object: obj1}
    console.log(object === object); // true because same object obj2 == obj2
    console.log(object.object === object); // false because two different object  as obj1 != obj2
    console.log(object.object === object.object); // true because same object obj1 == obj1
    console.log(object.object.object === object.object); // false because undefined != obj1 

Upvotes: 0

Barmar
Barmar

Reputation: 781068

After the first assignment, you have the following:

object = { object: undefined }

The second assignment creates a new object, whose object property contains the previous value of window.object, and assigns this to object. So now you have:

object = { object: { object: undefined } }

So at this time, object.object is { object: undefined }, which is not the same as object.

Similar things happen with the array example.

If you want to create a self-referential object, you need to do:

object = {};
object.object = object;

This doesn't create a new object in the second assignment, it modifies the original object. Then you could do:

console.log(object.object.object.object === object); // true

For an array, it would be:

array = [];
array[0] = array;
console.log(array[0][0][0][0][0] === array); // true

Upvotes: 2

Pointy
Pointy

Reputation: 413737

There are two assignments to window.object. The first one creates a new object and assigns it to window.object. That one has a property called "object" whose value is undefined, because at the time the object literal is evaluated window.object is undefined.

The second assignment instantiates a new object. That one also has a property called "object", whose value is the object created in the first assignment.

Thus, window.object is not the same object as window.object.object.

The key is that in an assignment like this:

object = { object: window.object };

The value of the "object" property in the object literal is evaluated before the assignment takes place.

Upvotes: 1

Related Questions