The delete operator in javascript detail _ basics

Source: Internet
Author: User
Tags eval

One, variable

When it comes to the delete operator in JavaScript, it's a matter of first figuring out the relationship between variables and attributes in JavaScript.

In JavaScript, variables and object attributes are very subtle and can be equated even more often, since JavaScript creates a global object in the browser, which is the Window object, before executing the script. All global variables are properties of this global object, and a activation object is created when the function is executed, and all local variables are properties of this activation object. This allows you to learn about JavaScript scopes and closures.

Copy Code code as follows:

The properties declared in the prototype cannot be deleted

var global = 1;
This.global; 1, you can access the global object through this
This.global2 = 2;
Global2; 12
function foo () {
var local = 36;
However, you cannot access activation directly,
Therefore, the local variables cannot be accessed through a foo.local approach
}

It seems that variables are equivalent to object attributes, but they are not, at least, different for the delete operator. My understanding is that variable declarations must be done through the Var statement, and that global variables that are 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 properties.

Two, delete operator

The delete operator is used to delete an object property. For a value of a reference type, it is also the deletion of the object property itself, and does not delete the object that the property points to. If you have questions, look at the values of the base type and reference type, or test the following code:

Copy Code code as follows:

var o = {};
var a = {x:10};
O.A = A;
Delete o.a; O.a property is deleted
Console.log (O.A); Undefined
Console.log (a.x); 10 because the {X:10} object is still referenced by a, so it will not be reclaimed

In addition, the delete o.x can write the delete o["X", both of which have the same effect.

Third, the variable can not be deleted

Variables declared through Var and functions declared by function have the Dontdelete attribute and cannot be deleted. Global variables declared without VAR (properties of global objects)

Copy Code code as follows:

var global = {
A:123,
B: {
c:1345
}
};
Delete global; Invalid
Console.log (Global)

obj = {
A:123
};
Delete obj; Delete obj global variable, Window object's Obj property
Console.log (obj);//obj is not defined

The attributes declared in the prototype and the attributes that were taken from the object cannot be deleted

The properties declared in the prototype prototype and the attributes that are contained in the object (in fact, these properties are also in the prototype prototype) can be considered to be of a dontdelete nature and cannot be deleted. For example

Copy Code code as follows:

The properties 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, the o.x defined in prototype will not be deleted even if the delete o.x is executed again

The object's own property cannot be deleted

var strings = "123456";
Console.log (strings.length);//6
Delete strings.length;
Console.log (strings.length);/still 6

V. Several exceptions under the eval statement

In the code for eval execution, variables declared by Var are the same as the global object with the normal var declaration variables, but they do not have the Dontdelete attribute and can be deleted. However, variables defined in the functions of the Eval code through VAR have dontdelete and cannot be deleted.

Copy Code code 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 for eval execution, the variable declared through Var, although it belongs to the global object with the normal var declaration variable,
However, they do not have dontdelete properties and can be removed.
Eval ("(function () {" +
"var x = 42;" +
"Delete x;"
"Return x;" +
"})();")
=> 42
Variables defined by Var within functions in the Eval code have dontdelete and cannot be deleted.

The return value of the delete

Delete is a normal operator and returns TRUE or false. Returns true if the property of the object being removed is present and returns false when it owns the Dontdelete. One feature here is that when an object property does not exist, it returns true, so the return value is not exactly the same as whether the deletion was successful or not.

Copy Code code 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 does not have Dontdelete, returns true

Delete o.y; True
O.Y; 12
O itself has no o.y property, so returns True
From here you can also see the existence of the prototype chain, the object itself and prototype properties are different

Delete o; False
GLOBAL.O has Dontdelete attribute 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 throw exceptions (violating the ECMAScript standard)

var x = 24;
Delete x + +; True
X 25
The deletion is the return value of X + + (24), not the property, so returns True

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.