Object copied to second property via reference persists even after original property deleted?

前端 未结 6 1871
情话喂你
情话喂你 2020-12-31 00:39

I thought objects are passed as reference. But when I delete b, it still exists in c. Please see this example:

This first part makes sense

6条回答
  •  攒了一身酷
    2020-12-31 01:41

    No, it's not a bug.

    The delete command doesn't delete the object that that the property is referencing, it only deletes the property. If the object is referenced from somewhere else (the other property in this case), the object is still alive.

    This is the same as if you have two variables pointing to the same object, and change the value of one variable:

    var a = { val: true };
    var b = a;
    a = null;
    console.log(b.val); // b still has a reference to the object
    

提交回复
热议问题