Javascript delete refers to a type object

  • 2020-03-26 21:46:42
  • OfStack

Here's an example:


var testVar = {
            a : {
                test : 1
            }
        },
            test1 = {},
            test2 = {};

        test1.a = testVar.a;
        test2.a = testVar.a;
/*
        delete test1.a;
        console.log(test1.a); // undefined
        console.log(test2.a); // Object {test: 1}
        console.log(testVar.a); // Object {test: 1}
*/
        delete testVar.a;
        console.log(test1.a); // Object {test: 1}
        console.log(test2.a); // Object {test: 1}
        console.log(testVar.a); // undefined

The test shows that javascript delete deletes not the referenced object but the pointer to the referenced object if the object is a reference type. So, even if I delete testvar. a, the object that test1.a points to is still not deleted.

For more on the principles of javascript delete keyword, I recommend:

(link: http://perfectionkills.com/understanding-delete/)

Translation:

(link: http://www.ituring.com.cn/article/7620)


Related articles: