Introduction to the delete operator in Javascript _ basic knowledge

Source: Internet
Author: User
This article mainly introduces the delete operator in Javascript in detail, focuses on the situations in which the delete operator can be used to obtain the return value of the delete operator. For more information, see I. Variables

When talking about the delete operator in javascript, we should first understand the relationship between variables and attributes in javascript.

In javascript, the relationship between variables and object attributes is very subtle, and may even be equivalent in many cases, because javascript creates a global object before executing the script, which is a window object in the browser, all global variables are the attributes of the global object. An activation object will be created when the function is executed. All local variables are the attributes of this activation object. You can take a look at the javascript scopes and closures.

The Code is as follows:


// Attributes declared in the prototype cannot be deleted.

Var global = 1;
This. global; // 1. You can use this to access global objects.
This. global2 = 2;
Global2; // 12
Function foo (){
Var local = 36;
// However, you cannot directly access activation,
// Therefore, the local variable cannot be accessed through foo. local.
}

It seems that variables are equivalent to object attributes. In fact, they are not the same, at least for the delete operator. In my understanding, the variable Declaration must be completed through the var statement. The global variables not declared through the var statement are all properties of the window object. This makes it easy to understand the relationship between variables and object attributes.

Ii. delete Operator

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

The Code is as follows:


Var o = {};
Var a = {x: 10 };
O. 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 write delete o ["x"], with the same effect.

3. Variables cannot be deleted.

Variables declared through var and functions declared through function have the dontdelete feature and cannot be deleted. Global variables not declared through var (attributes of the Global Object)

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. Attributes declared in the prototype and attributes of the object cannot be deleted.

Attributes declared in prototype and attributes of objects (in fact, these attributes are also in prototype) can be considered as having the dontdelete feature and cannot be deleted. For example

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, the o. x defined in the constructor

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

// Attributes 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 the variables declared through var belong to the global object with the normal var declaration variables, they do not have the dontdelete feature and can be deleted. However, variables defined by var in functions in eval Code have dontdelete and cannot be deleted.

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 through var belong to the global object,
// 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.

Vi. Return Value of delete

Delete is a common operator and returns true or false. If the property of the object to be deleted exists and has dontdelete, false is returned. Otherwise, true is returned. One feature here is that true is returned if the object property does not exist, so the return value is not exactly the same as whether the deletion is successful or not.

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
// If o. x exists and no dontdelete exists, true is returned.

Delete o. y; // true
O. y; // 12
// O itself does not have the o. y attribute, so true is returned.
// You can also see the existence of the prototype chain from here. The attributes of the object and prototype are different.

Delete o; // false
// Global. o has the dontdelete feature, so false is returned.

Delete undefinedproperty; // true
// Global does not have an attribute named undefinedproperty. Therefore, true is returned.

Delete 42; // true
// 42 is not an attribute, so true is returned. Some implementations throw an exception (in violation of the ecmascript Standard)

Var x = 24;
Delete x ++; // true
X; // 25
// The deleted value is the return value of x ++ (24), which is not an attribute. Therefore, true is returned.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.