How to use the Delete method in JavaScript

Source: Internet
Author: User
Tags constructor eval garbage collection object object

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.

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:

The code is as follows Copy Code

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:

The code is as follows Copy Code

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.

The code is as follows Copy Code
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 are lost, so the delete o.x is "equivalent" to deleting the object pointed to by o.x, but this action is not ECMAScript standard, that is to say, Even if an implementation does not delete object objects at all, it is not a violation of the ECMAScript standard.

"Delete attributes instead of deleting objects" can be confirmed by the following code.

The code is as follows Copy Code
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.


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.

  code is as follows copy code

var x = 36;
Delete x;
x;    //, X not deleted

Y = 12;
Delete y;
y;    //undefined

Function foo () {return 42;}
Delete foo;
Foo (); //
But with one exception is that in code executed through eval, variables declared through Var, although they belong to the global object as normal var declaration variables, do not have the Dontdelete attribute and can be deleted.

Eval ("var x = 36;");
x;    //
Delete x;
x;    //undefined
But there's one more exception, Variables defined by Var within functions in the Eval code have dontdelete and cannot be deleted.

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

The return value of the delete

Delete is a normal operator and returns TRUE or false. The rule is: when the attributes of the object being removed exist and have Dontdelete
Returns FALSE, 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.

The code is as follows Copy Code

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

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.

The code is as follows Copy Code

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:

The code is as follows Copy Code

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.

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

JavaScript garbage collection is the browser's own processing, for strings, objects, data, these are not fixed size, they must be dynamically allocated memory, but when it is recycled? JavaScript uses the same method of garbage collection as Java. Programmers cannot intervene too much, and the delete action in JavaScript is used to delete a domain in an object or a member of an array, and if you want to delete an object, you only need to set the object to null or undefined.

Use of Delete

The code is as follows Copy Code

Delete Obj.prop;

Delete obj[' prop '];

Delete Arr[i];

Example

The code is as follows Copy Code

<script language= "JavaScript" >
var obj = new Object ();
Obj.classname = ' instance ';
obj.parent = ' Object ';
Obj.des = ' some words ';
for (key in obj)
Alert (Obj[key]);
Delete obj.des;
for (key in obj)
Alert (Obj[key]);
</script>


§deleting variables via eval (delete variable via eval)

The most interesting is the Eval feature, on the other hand ECMAScript can technically allow us to delete properties that cannot be deleted. The declaration of a function in the same context can be overridden by a variable of the same name.

The code is as follows Copy Code
function X () {}
var x;
typeof X; "Function"

Note the function declaration takes precedence, overriding the variable with the same name (or, in other words, having the same attribute in the variable object). This is because a function declaration is an instance that, after the variable reputation (Variable declarations), is allowed to overwrite not only the function declaration that replaces the value of the former property, it can also replace its properties.

If we use Eval to declare a function then we can still replace the corresponding attribute, because the variable declaration created in Eval has no dontdelete, the following example deletes the existing Dontdelete attribute essentially.

The code is as follows Copy Code

var x = 1;
/* Can ' t Delete, ' x ' has dontdelete * *

Delete x; False
typeof X; "Number"
Eval (' function X () {} ');
/* ' X ' property now references function, and should have no dontdelete * *

typeof X; "Function"
Delete x; Should be ' true '
typeof X; should be "undefined"

Unfortunately, I try all kinds of scenes that can't work, and there's a chance I'll be overlooked.

Other properties that cannot be deleted

The properties declared in prototype cannot be deleted except for the built-in attributes (that is, predefined properties) that are mentioned above.

The code is as follows Copy Code


function C () {this.x = 42;}
c.prototype.x = 12;
C.PROTOTYPE.Y = 13;

var o = new C ();
o.x; 42, the o.x defined in the constructor

Delete o.x; True deletes the x that is defined by itself
o.x; The o.x defined in prototype is not deleted even if the delete o.x is executed again

Delete o.y; True, because O itself has no o.y attribute, Y exists in the prototype chain, which means that the object itself and the prototype property are different
O.Y; 13

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.