Home > Web Front-end > JS Tutorial > Detailed introduction to delete operator in Javascript_Basic knowledge

Detailed introduction to delete operator in Javascript_Basic knowledge

WBOY
Release: 2016-05-16 16:46:01
Original
1126 people have browsed it

1. Variables

Speaking of the delete operator in JavaScript, we must first understand the relationship between variables and attributes in JavaScript.

In JavaScript, the relationship between variables and object properties is very subtle, and they can even be equated in many cases, because JavaScript will create a global object before executing the script, which is the window object in the browser, and all global variables are this Attributes of the global object. When executing a function, an activation object will also be created. All local variables are attributes of this activation object. You can learn about these javascript scopes and closures.

Copy code The code is as follows:

//Attributes declared in the prototype 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 through foo.local
}

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

2. delete operator

The delete operator is used to delete object attributes. For reference type values, it also deletes the object attribute itself and does not delete the object pointed to by the attribute. If you have any questions, you can look at the values ​​of basic types and reference types, or test the following code:

Copy the code The code is as follows:

var o = {};
var a = { x: 10 };
o.a = a;
delete o.a; // The o.a attribute is deleted
console. log(o.a); // undefined
console.log(a.x); // 10, because the { x: 10 } object is still referenced by a, it will not be recycled

In addition, delete o.x can also be written as delete o["x"], both of which have the same effect.

3. Variables cannot be deleted

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

Copy code The code is as follows:

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

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

4. The attributes declared in the prototype and the attributes of 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 dontdelete feature and cannot be deleted. For example,

Copy code The code is as follows:

//Attributes declared in the prototype cannot be deleted

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

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

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

//The properties of the object cannot be deleted

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

5. Several exceptions under the eval statement

In the code executed by

eval, although variables declared with var belong to the same global object as normal var-declared variables, they do not have the dontdelete feature and can be deleted. However, variables defined through var within the function in the eval code have dontdelete and cannot be deleted.

Copy code The code 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 variables declared by var belong to the same global object as the normal var declared variables,
// they do not have the dontdelete feature and can be deleted.
eval("(function () {"
" var x = 42;"
" delete x;"
" return x;"
"})();")
// => 42
// The variables defined by var within the function in the eval code have dontdelete and cannot be deleted.

6. Return value of delete

delete is a normal operator and will return true or false. Returns false when the property of the deleted object exists and has dontdelete, otherwise it returns true. One feature here is that true is returned even when the object attribute does not exist, so the return value is not completely equivalent to whether the deletion is successful or not.

Copy code The code is as follows:

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 and there is no dontdelete, return true

delete o.y; // true
o.y; // 12
// o itself There is no o.y attribute, so return true
// From here you can also see the existence of the prototype chain. The object's own attributes and the prototype attributes are different

delete o; // false
// global .o has the dontdelete attribute so it returns false

delete undefinedproperty; // true
// global has no property named undefinedproperty so it returns true

delete 42; // true
// 42 is not an attribute so returns true. Some implementations will throw exceptions (violating ecmascript standards)

var x = 24;
delete x; // true
x; // 25
// x is deleted The return value (24) is not an attribute, so it returns true
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template