Javascript learning notes delete Operator

Source: Internet
Author: User

I. Syntax

The expression after delete must be referenced by an attribute, for example
Var o = {a: 1 };
Delete o. a; // here, o. a is a reference to attribute a of object o.

A separate attribute name can be used only in the with statement.Copy codeThe Code is as follows: with (o ){
Delete;
}

Ii. Return Value of delete

Delete is a common operator and returns true or false. The rule is: if the property of the deleted object exists and cannot be deleted, 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.Copy codeThe Code is as follows: var o = {a: 1 };
Delete o. a; // return true
Var B = 2;
Delete B; // false is returned. The ECMA rule stipulates that variables declared using var and function cannot be deleted.

Iii. under which circumstances cannot delete

The variables declared by var AND function mentioned in the previous example cannot be deleted, but the implicit declaration can be deleted.Copy codeCode: function c () {return 12 ;}
Delete c; // return false
D = function () {return 12 ;}
Delete d; // return true

You cannot delete attributes inherited from the prototype chain, but you can delete attributes from the prototype chain.Copy codeCode: function Foo (){}
Foo. prototype. bar = 42;
Var foo = new Foo ();
Delete foo. bar; // returns true, but does not work
Alert (foo. bar); // alerts 42, attributes are inherited
Delete Foo. prototype. bar; // delete the attribute bar on the prototype.
Alert (foo. bar); // alerts "undefined", the property does not exist and cannot be inherited

Iv. Special Cases Copy codeThe Code is as follows: any variable declared through var and function in the Code executed by eval can be deleted
Eval ("var a = 1 ");
Delete;
Alert (a); // an undefined error is reported.

If the declaration is performed in the closure of eval Execution Code, the variable cannot be deleted.
Eval ("(function () {var a = 1; delete a; return a ;}) ()"); // 1
5. delete array elements

Deleting elements from an array does not affect the length of the array.Copy codeThe Code is as follows: var arr = ['yuin', 'suhuany', 'baby'];
Delete arr [0];
Alert (arr. length); // alert 3

The deleted key value does not belong to an array, but can still be accessed. Its value is undefined.Copy codeThe Code is as follows: var arr = ['yuin', 'suhuany', 'baby'];
Delete arr [0];
0 in arr; // false
Alert (arr [0]); // undefined
Arr [0] ===undefined; // true

Compare and directly assign the key value undefinedCopy codeThe Code is as follows: var arr = ['yuin', 'suhuany', 'baby'];
Arr [0] = undefined;
0 in arr; // true
Alert (arr [0]); // undefined
Arr [0] ===undefined; // true

It can be seen that the delete operation only deletes the key value attribute from the array, and the array itself is an object, which is understandable. If you want to retain the key value, you can use undefined to assign a value.

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.