JS Input/Output Solution 5

What will be the output?

1
2
3
4
5
    var obj = {a: 5};
    var obj2 = Object.create(obj);
    delete obj2.a;

    console.log(obj2.a);    //?

Solution:

1
2
3
4
5
    var obj = {a: 5};
    var obj2 = Object.create(obj);
    delete obj2.a;

    console.log(obj2.a);    //5

There are two things happening here -

  • We are copying obj to obj2 using Object.create(). Object.create() copies the existing object to the prototype( _proto__) of new obj_(obj2 in this case).

    Read more

1
2
3
4
5
6
7
8
9
var obj2 = Object.create(obj);

// obj2 can be visualised as

obj2 = {
     __proto__: {
         a: 5
     }
}
  • Deleting the property using delete operator. This operator only deletes the value which is present directly on the object and does not delete any value present on the prototype( **_proto___) of an object. That is why deleting property a does not have any effect.