2

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 ???

3
  • 1
    Why define the variable twice, overwriting it? Edit: ok ;) It does not work without them Commented Jul 2, 2015 at 20:10
  • 2
    because first time window.object is undefined Commented Jul 2, 2015 at 20:11
  • 1
    If you know the answer why did you post the question? Commented Jul 2, 2015 at 20:13

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

So there are two objects ?
Yes, there are two objects. Why wouldn't there be? Every distinct object literal expression creates a new object.
0

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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.