1. Variables
When it comes to the delete operator in javascript, we should first figure out the relationship between variables and attributes in javascript.
In JavaScript, the relationship between variables and object attributes is very subtle, and it can even be equated many times, because JavaScript will create a global object before executing the script, which is a window object in the browser. All global variables are attributes of this global object. When executing the function, an activation object will also be created. All local variables are attributes of this activation object. You can learn about these javascript scope and closures.
The code copy is as follows:
//The attributes declared in the prototype cannot be deleted
var global = 1;
this.global; // 1, you can access global objects through this
this.global2 = 2;
global2; // 12
function foo() {
var local = 36;
// However, activation cannot be accessed directly.
// Therefore, local variable cannot be accessed through foo.local
}
It seems that variables are equivalent to object properties, but in fact, they are not the same, at least for the delete operator. My understanding is that variable declarations must be completed through var statements. Global variables that are not declared through var statements are all properties of window objects. This makes it easy to understand the relationship between variables and object properties.
2. delete operator
The delete operator is used to delete object properties. For reference type values, it also deletes the object properties themselves, and does not delete the object pointed to by the property. If you have any questions, you can look at the values of the basic type and reference type, or test the following code:
The code copy is as follows:
var o = {};
var a = { x: 10 };
oa = a;
delete oa; // oa attribute is deleted
console.log(oa); // undefined
console.log(ax); // 10, because the { x: 10 } object is still referenced by a, so it will not be recycled
In addition, delete ox can also be written as delete o["x"], and the effects of both are the same.
3. Variables cannot be deleted
Variables declared by var and functions declared by function have the dontdelete feature and cannot be deleted. Global variables not declared via var (properties of global objects)
The code copy is as follows:
var global = {
a: 123,
b: {
c: 1345
}
};
delete global; //Invalid
console.log(global)
obj = {
a: 123
};
delete obj; // Delete obj global variables, obj attribute of window object
console.log(obj);//obj is not defined
4. The attributes declared in the prototype and the attributes that come with the object cannot be deleted.
The attributes declared in the prototype prototype and the attributes that come with the object (in fact, these attributes are also in the prototype prototype) can be considered to have the characteristics of dontdelete and cannot be deleted. For example
The code copy is as follows:
//The attributes declared in the prototype cannot be deleted
function obj() {
this.x = 1;
}
obj.prototype.x = 2;
var o = new obj();
console.log(ox); // 1, ox defined in the constructor
delete ox;
console.log(ox); // 2, ox defined in prototype, even if delete ox is executed again, it will not be deleted.
//The attributes that come with the object cannot be deleted
var strings = "123456";
console.log(strings.length);//6
delete strings.length;
console.log(strings.length);//It's still 6
5. Several exceptions under the eval statement
In the code executed by eval, although variables declared by var belong to global objects as normal var declared variables, they do not have the dontdelete attribute and can be deleted. However, the variables defined by var in the function in the eval code have dontdelete and cannot be deleted.
The code copy is as follows: 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 executed by eval, although the variable declared by var belongs to the global object as normal var declared variables,
// But they do not have the dontdelete feature and can be deleted.
eval("(function () {" +
" var x = 42;" +
" delete x;" +
" return x;" +
"})();")
// => 42
// The variable defined by var in the function in the eval code has dontdelete and cannot be deleted.
6. The return value of delete
delete is a normal operator and will return true or false. Return false when the property of the deleted object exists and has dondelete, otherwise true. One feature here is that when the object attribute does not exist, it returns true, so the return value is not exactly equivalent to whether the deletion is successful or not.
The code copy is as follows:
function c() {
this.x = 42;
}
c.prototype.y = 12;
var o = new c();
delete ox; // true
ox; // undefined
"x" in o; // false
// Ox exists and does not have dontdelete, returns true
delete oy; // true
oy; // 12
// o itself does not have an oy attribute, so it returns true
// From here you can also see the existence of the prototype chain. The object's own attributes and prototype attributes are different.
delete o; // false
// global.o has the dontdelete feature so returns false
delete undefinedproperty; // true
// global does not have a property named undefinedproperty so returns true
delete 42; // true
// 42 is not a property so returns true. Some implementations will throw exceptions (violate the ecmascript standard)
var x = 24;
delete x++; // true
x; // 25
// What is deleted is the return value of x++ (24), not a property, so it returns true