Delete and delete operator _ javascript skills in javascript

Source: Internet
Author: User
This article mainly introduces related information about the delete and delete operators in javascript. For more information, see why we can delete the attributes of an object:

var x = { a: 1 };delete x.a; // truex.a; // undefined

However, you cannot delete a variable:

var x = 1;delete x; // false;x; // 1

You cannot delete a function either:

function x() {};delete x; // false;typeof x; // "function"

Note: delete returns false only when a property cannot be deleted.

Each attribute has zero to multiple Internal attributes, such as * ReadOnly, DontEnum, DontDelete, and Internal **. You can think of them as tags-a property may have or may not have a special internal property. In today's discussion, we are interested in DontDelete.

When declaring variables and functions, they become Variable objects-either activated objects (in Function Code) or global objects (in global code) -- attributes. These attributes are generated along with the internal attribute DontDelete. However, do not generate DontDelete for any explicit/implicit values. This is why we can delete some attributes but cannot delete others.

var GLOBAL_OBJECT = this;

/* 'Foo' is an attribute of the global object. It is generated through variable declaration. Therefore, it has the internal attribute DontDelete.

This is why it cannot be deleted */

var foo = 1;delete foo; // falsetypeof foo; // "number"/* 'bar

'Is an attribute of a global object. It is generated through variable Declaration, so it has the DontDelete sub-

This is why it cannot be deleted */

function bar() {};delete bar; // falsetypeof bar; // "function"

/* 'Baz' is also an attribute of the global object,

However, it is generated by assigning values to attributes, so there is no DontDelete

This is why it can be deleted */

GLOBAL_OBJECT.baz = "baz";delete GLOBAL_OBJECT.baz; // truetypeof GLOBAL_OBJECT.baz; // "undefined"

1.5 building and DontDelete | Build-ins and DontDelete

So this is the reason for all this: a special internal attribute of an attribute controls whether the attribute can be deleted. Note: Some attributes of the built-in object have internal attributes DontDelete, so they cannot be deleted. Special arguments variables (as we know, attributes of the activated object) have DontDelete; the length attribute of any function instance (return parameter length) also has DontDelete:

(Function () {// You cannot delete 'arguments 'Because DontDelete delete arguments; // false; typeof arguments; // "object" // You cannot delete the length of a function, because DontDelete function f () {}; delete f. length; // false; typeof f. length; // "number "})();

Attributes associated with the function arguments also have DontDelete and cannot be deleted.

(function(foo,bar) {  delete foo; // false  foo; // 1  delete bar; // false  bar; // "bah"}) (1,"bah");

1.6 Undeclared variable assignment | Undeclared assignments

You may remember that un-declared variable assignments will become attributes of the global object, unless this attribute is found elsewhere in the scope chain. Now we know the difference between attribute assignment and variable declaration-the latter generates DontDelete and the former does not -- That is why undeclared variable assignment can be deleted.

Var GLOBAL_OBJECT = this;/* generate the attributes of the global object through the variable declaration, with DontDelete */var foo = 1;/* generate the attributes of the global object through the undeclared variable value assignment, no DontDelete */bar = 2; delete foo; // falsedelete bar; // true

Note: The internal attribute is determined when the attribute is generated. The subsequent value assignment process does not change the internal attribute of the existing attribute. Understanding the difference is important.

/* The DontDelete */function foo () {};/* value assignment process after 'foo' is created does not change the internal attributes of existing attributes, when DontDelete still exists */foo = 1; delete foo; // false; typeof foo; // "number"/* but assigned a non-existent attribute, an Attribute without internal attributes is created, so no DontDelete */this. bar = 1; delete bar; // true; typeof bar; // "undefined"

Summary:

Both variables and function declarations are attributes of the Activation Global object.

An attribute has an internal attribute. One -- DontDelete is used to determine whether an attribute can be deleted.

Variables and function declarations in global code or function code generate the DontDelete attribute.

Function parameters are also attributes of the activated object and also have DontDelete.

Delete an attribute in an object: delete object. Member

Only existing members can be deleted.

Delete is not allowed only for global variables declared by var.

You can delete global members added by using window. Or window [""].

Ps: delete operator in Javascript

Delete is one of the less frequently used operations in Javascript. However, in some cases, delete operations are required when delete or clear operations are required. In this article, we will explore in depth how to use it and how it works.

The purpose of deletion, as you think, is to delete something. More specifically, it will delete the attributes of the object, as shown in the following example:

var Benjamin = {  "name": "zuojj",  "url" : "http://www.zuojj.com"};delete Benjamin.name;//Outputs: Object { url: "http://www.zuojj.com" }console.log(Benjamin);

The delete operator does not delete common variables, as shown in the following example:

var benjamin = "http://www.zuojj.com";delete benjamin;//Outputs: "http://www.zuojj.com"console.log(benjamin);

However, it can delete global variables because they are actually attributes of Global Objects (windows in the browser.

// Because var isn't used, this is a property of windowbenjamin = "zuojj";delete window.benjamin;// ReferenceError: benjamin is not definedconsole.log(benjamin);

The delete operator also has a return value. If an attribute is successfully deleted, true is returned. If the attribute cannot be deleted because it cannot be written, false is returned, or an error is thrown in strict mode.

var benjamin = {  "name": "zuojj",  "url" : "http://www.zuojj.com"};var nameDeleted = delete benjamin.name;// Outputs: trueconsole.log(nameDeleted);"use strict";var benjamin_ = "zuojj";//Outputs: Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.delete benjamin_;

You may not know when to use the delete operator. The answer is, as long as you really want to delete an attribute from the object.

Sometimes, Javascript development does not delete an attribute, but sets this attribute value to null, as shown below:

var benjamin = {  "name": "zuojj",  "url" : "http://www.zuojj.com"};benjamin.name = null;

Although this effectively removes the attribute from the original value, the attribute still exists on the object. You can see the following:

// Outputs: Object { name: null, url: "http://www.zuojj.com" }console.log(benjamin);

At the same time, loop operations such as in and for in will not report the existence of the null attribute. If you use an object, you may use these methods to check an object, you may want to make sure that you delete any unnecessary attributes.

Finally, you should remember that deleting the attribute does not destroy the attribute value. For details, refer to the following example:

var name   = "zuojj",    benjamin = {};benjamin.name = name;delete benjamin.name;//Outputs: "zuojj"console.log(name);

Here, name and benjamin. name are mapped to the same value. As you can see, deleting benjamin. name does not affect name.

The above is my overview of the delete operator. If not, you are welcome to criticize and correct it.

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.