JavaScript Delete operator application Instance _javascript tips

Source: Internet
Author: User
Tags eval object object
Today, we found the delete operator when I looked at the prototype code.
Copy Code code as follows:

Unset:function (key) {
var value = This._object[key];
Delete This._object[key];
return value;
}

Checked the manual,
Delete operator
Deletes an attribute from the object, or deletes an element from the array.
Delete expression
The expression parameter is a valid JScript expression, usually a property name or an array element.
Description
Returns false if the result of the expression is an object and the property specified in expression exists and the object does not allow it to be deleted.
In all other cases, returns TRUE.
Seeing "Removing an element from an array" feels good, but in FF, it seems that you can only delete the value of that element, not the element itself. However, it is OK to remove a property from the object.
Google again, found that there is an article said very detailed, reproduced to avoid forgetting:
JavaScript variables
In fact, in JavaScript, variables = Object properties, because JavaScript creates a global object before executing the script, all global variables are properties of the global object, and a activation object is created when the function is executed. All local variables are attributes of this activation object. The following example:
Copy Code code as follows:

var global = 42;
This.global; 42, you can access the global object through this
This.global2 = 12;
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
}

Object deleted by the delete operator
C + + also has the delete operator, which deletes the object that the pointer points to. For example:
Copy Code code as follows:

C++
Class Object {
Public
Object *x;
}
Object o;
o.x = new Object ();
Delete o.x; Object on line new will be freed

But the JavaScript delete is different from C + +, it does not delete the object that o.x points to, but deletes the O.x property itself.
Copy Code code as follows:

Javascript
var o = {};
o.x = new Object ();
Delete o.x; The object object of the previous line new is still present
o.x; Undefined,o's attribute named X is deleted.

In actual JavaScript, after delete o.x, object objects are garbage collected because they have lost references, so the delete o.x is "equivalent" The object pointed to by o.x is deleted, but this action is not a ECMAScript standard, that is, even if an implementation does not delete the object at all, it is not a violation of the ECMAScript standard.
"Delete attributes instead of deleting objects" can be confirmed by 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
O.A; Undefined
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.
Performing a delete on a variable
Because the variable is also Global or a property of the activation object, the delete operation on the variable is the same result.
Copy Code code as follows:

var global = 42;
Delete global; Delete Global.global
function foo () {
var local = 36;
Delete local; Delete activation.local
}

Properties that can be deleted and properties that cannot be deleted
Not all attributes can be delete. For example, a property declared in prototype cannot be delete:
Copy Code code as follows:

function C () {this.x = 42;}
c.prototype.x = 12;
var o = new C ();
o.x; 42, the o.x defined in the constructor
Delete o.x;
o.x; The o.x defined in prototype is not deleted even if the delete o.x is executed again

The predefined properties of the object cannot be deleted either. This type of attribute can be considered to have dontdelete characteristics.
Copy Code code as follows:

var re =/abc/i;
Delete re.ignorecase;
Re.ignorecase; True, ignorecase cannot be deleted

Variables that can be deleted and variables that cannot be deleted
Variables declared through Var and functions declared by function have the Dontdelete attribute and cannot be deleted.
Copy Code code as follows:

var x = 36;
Delete x;
X X is not deleted
y = 12;
Delete y;
Y Undefined
function foo () {return 42;}
Delete foo;
Foo (); 42

One exception, however, is that in code executed through eval, variables declared through 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.
Copy Code code as follows:

Eval ("var x = 36;");
X 42
Delete x;
X Undefined

But there is one exception to this, the variables defined by VAR in the Eval code are dontdelete and cannot be deleted.
Copy Code code as follows:

Eval ("(function () {var x = 42; Delete x; return x; })();");
Return 42

The return value of the delete
Delete is a normal operator and returns TRUE or false. Rule is: Returns False when the property of the object being removed is present and has dontdelete, otherwise returns true. 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 existence and no dontdelete, return true
Delete O.Y; True
O.y//
//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 the prototype properties are different
Delete O ; False
//GLOBAL.O has Dontdelete attribute so returns false
Delete Undefinedproperty;//True
//Global does not have a name of Undefinedprop Erty Property therefore returns true
Delete;//True
//42 is not an attribute so returns TRUE. Some implementations throw exceptions (violating the ECMAScript standard)
var x = 24;
Delete x + +//true
X;//
//deleted x + + return value (24), not property, so return 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.