The delete operator before the understanding is not too deep, encountered some problems found that they can not understand, after the search for friends and relatives found that the original knowledge is not solid enough, so you boys still have a lot of practice, can not be surprised Ah!
OK, let's take a look at the topic:
1 //Example One2 varo = {x:1 }; 3 Deleteo.x;//true4o.x;//undefined5 6 //Example Two7 varx = 1; 8 DeleteX//false9X//1Ten One //example Three A functionx () {} - DeleteX//false - typeofX//"function"
As you can see from the three examples above, the delete operator cannot delete a variable declared through VAR or delete a function, but you can delete the properties of the object that displays the declaration. What is this for? For example one should be easy to understand, for example two or three
You need to understand some of the concepts in JavaScript. Take a look at the following concepts:
1. Code type
There are three types of executable code in ECMAScript: Global Code, function code, and eval code, which is executed in eval
1 var x=1; // 2function3var y=2; // 4 eval ("var z=3"); // 56 eval ("function Evaltest () {}"); //
2. Execution context
When JavaScript code executes, it always runs in a certain context, and the execution context is a somewhat abstract entity that helps us understand how scopes and variable instantiation work. For three types of executable code, each has a context for execution.
When a function executes, it can be said to control the execution context into the function code. When the global code executes, it enters the execution context of the global code. The execution context logically comes from a stack. First of all, it may have its own role
The global code of the domain, the code may call a function, it has its own scope, the function can call another function, and so on. Even if the function invokes itself recursively, each invocation enters a new execution context.
3, Activation object (active Object)/variable object (Variable object)
Each execution context has a variable object (variable objects) inside it. Like the execution context, Variable object is an abstract entity used to describe the mechanism of variable instantiation. Interestingly, the variables and functions declared in the code are actually treated as
The properties of this variable object are added. When entering the execution context of the global Code, a global object is used as a variable object. This is why the variable or function declared in the global scope becomes the property of the global object.
Global variables become properties of global objects, but what about local variables defined in function code? The behavior is actually very similar: it becomes the property of the Variable object. The only difference is that in the function code,
The variable object is not a global object, but a so-called activation object (Activation object). Each time the function code enters the execution scope, an Activation object (Activation object) is created.
Not only the variables and functions in the function code are the properties of the active object, but each parameter of the function (the name corresponding to the formal parameter) and a specific arguments object are also. Note that the activation object is an internal mechanism that is not actually accessed by the program code.
4. Attribute Properties
Now that the variables are clear (they become attributes), the only remaining concept that needs to be understood is attribute attributes. Each property has 0 or more attributes from the following set of properties--readonly, Dontenum, dontdelete and internal, you can recognize
For them is a token, an attribute that is optional. Now we only discuss the Dontdelete feature.
When the declared variables and functions become properties of a variable object-either the Activation object (function code) or the global code, these attributes are created with the Dontdelete attribute. However, any explicit (or implied) creation of the genus
Sex does not have dontdelete characteristics. That's why some of our properties can be deleted, some can't.
5. Built-in attributes and Dondelete
A unique attribute (Dontdelete) in the property controls whether this property can be deleted. Note that the built-in properties of the object (that is, the object's predefined properties) have the Dontdelete attribute and cannot be deleted. Specific arguments variables (or, as we now know
, the property of the active object), and the length property of any function instance also has the Dontdelete attribute. The created property corresponding to the function parameter also has the Dontdelete attribute, and therefore cannot be deleted. Undeclared assignment creates a property that can be deleted on a global object.
1 (function23Delete//4// 5 6 Delete//7// 89
6. Eval Code
The variable or method created in Eval is special, without the Dontdelete attribute, which means it can be deleted.
1Eval ("var x = 1;"); 2Console.log (x);//13 Deletex;4Console.log (typeofx);//undefined5 6Eval ("function test () {var x=1; Console.log (delete x);/* false */;return 1;} "); 7Console.log (Test ());//18 Deletetest;9Console.log (typeofTest);//undefined; Note When you execute this line of code in Chrome's console, test is not deleted typeof test is function, running test in the browser is actually deleted.
Note that the variable or method created in eval does not include variables or methods inside the method, such as the local variable x in the test function in the above code, which is still consistent with the previous argument and cannot be deleted.
Also explain a point of knowledge:
In the actual JavaScript, delete o.a removes the O.a property itself, rather than deleting the object that the O.A points to, as shown in the following example.
1 var o =2var a = {x:103 o.a =4Delete c10>//5//6// 10, because {X:10} object is still referenced by a, So they won't be recycled .
The prototype properties of a function cannot be deleted:
1 functionC () { This. x = 42; } 2c.prototype.x = 12; 3 4 varo =NewC ();5o.x;//42, o.x defined in the constructor6 7 Deleteo.x;//true to delete a self-defined x8o.x;//o.x, defined in prototype, will not be deleted even if the delete o.x is executed again
Reference Original address: http://perfectionkills.com/understanding-delete/
In-depth understanding of JS in the delete operator