The delete operator in Javascript is described in detail

  • 2020-03-30 03:13:28
  • OfStack

A, variables,

Speaking of the delete operator in javascript, it's still important to understand the relationship between variables and properties in javascript.

Javascript, variable and object property relation is very subtle and can even be equate a lot of time, because the javascript before executing the script will create a global object, in the browser's window object, all global variables are the global properties of the object, when performing a function will also create a activation object, all the local variables are the activation properties of the object. So you can look at javascript scopes and closures.

 
//Properties declared in a stereotype cannot be deleted

var global = 1;
this.global; //1, the global object can be accessed through this
this.global2 = 2;
global2; // 12
function foo() {
var local = 36;
//However, activation cannot be accessed directly,
//Therefore, local variables cannot be accessed in the foo.local way
}

It seems that variables are equivalent to object properties, but they are not, at least not for the delete operator. My understanding is that variable declaration must be done by var statement, and global variables not declared by var statement are all properties of window object. This makes it easy to understand the relationship between variables and object properties.

Delete operator

The delete operator is used to delete an object property. For the value of the reference type, it also deletes the object property itself, not the object to which the property points. If in doubt, look at the values of the base and reference types, or test the following code:

 
var o = {};
var a = { x: 10 };
o.a = a;
delete o.a; //The o.a attribute is removed
console.log(o.a); // undefined
console.log(a.x); // 10,  because { x: 10 }  The object is still being  a  Reference, so it will not be recycled 

Alternatively, delete o.x can also be written as delete o["x"], with the same effect.

Variables cannot be deleted

Variables declared via var and functions declared via function have the dontdelete property and cannot be deleted. Global variables not declared via var (properties of global objects)

 
var global = {
    a: 123,
    b: {
        c: 1345
    }
};
delete global; // invalid 
console.log(global)

obj = {
    a: 123
};
delete obj; //Delete the obj property of the obj global variable, the window object
console.log(obj);//obj is not defined

 

The declared properties in the prototype and the properties of the object itself cannot be deleted

The declared properties in prototype and the properties that come with the object (which are also in prototype) can be considered to have dontdelete properties and cannot be deleted. For example,

 
//Properties declared in a stereotype cannot be deleted

function obj() {
    this.x = 1;
}
obj.prototype.x = 2;

var o = new obj();
console.log(o.x); //1, the o.x defined in the constructor

delete o.x;
console.log(o.x); //2. The o.x defined in prototype will not be deleted even if delete o.x is executed again

//The properties that come with the object cannot be deleted

var strings = "123456";
console.log(strings.length);//6
delete strings.length;
console.log(strings.length);// Is still 6

A few exceptions to the eval statement

In the code eval executes, variables declared through var, although they belong to the global object as normal var declared variables, do not have the dontdelete property and can be deleted. But a variable defined by var inside a function in eval's code has dontdelete and cannot be deleted.

eval("var x = 42;");
x; // => 42
delete x;
x; // => referenceerror: x is not defined
eval("function f() { return 12; }");
f(); // => 12
delete f;
f(); // => referenceerror: f is not defined
//In the code eval executes, variables declared through var are part of the global object, even though they are part of the normal var declaration.
//But they do not have the dontdelete feature and can be deleted.
eval("(function () {" +
    " var x = 42;" +
    " delete x;" +
    " return x;" +
    "})();")
// => 42
//Variables in eval's code defined by var have dontdelete and cannot be deleted.

Return value of delete

Delete is a normal operator that returns true or false. Returns false when the property of the deleted object exists and has dontdelete, or true otherwise. One of the features here is that an object property returns true when it does not exist, so the return value is not exactly the same as a successful deletion.

 
function c() {
    this.x = 42;
}
c.prototype.y = 12;
var o = new c();

delete o.x; // true
o.x; // undefined
"x" in o; // false
//O.x exists without dontdelete and returns true

delete o.y; // true
o.y; // 12
//O has no o.y attribute of its own, so it returns true
//Here, too, you can see that the prototype chain exists, and that the properties of the object itself are different from the prototype properties

delete o; // false
//Global.o has the dontdelete feature so returns false

delete undefinedproperty; // true
//Global has no property named undefinedproperty and therefore returns true

delete 42; // true
//42 is not a property so returns true. Some implementations throw exceptions (violating the ecmascript standard)

var x = 24;
delete x++; // true
x; // 25
//  What was deleted was x++ The return value of the (24) , is not a property, so returns true


Related articles: