Delete and delete operators in JavaScript _javascript tips

Source: Internet
Author: User

So, why can we delete an object's properties:

var x = {a:1};
Delete x.a; True
x.a;//undefined

But you cannot delete a variable:

var x = 1;
Delete x; false;
X 1

You cannot delete a function:

function X () {};
Delete x; false;
typeof X; "Function"

Note: Delete returns false only if a property cannot be deleted.

Each property has 0 to multiple such as internal properties--*readonly,dontenum,dontdelete and internal**. You can think of them as tags--a property may or may not have a particular intrinsic attribute. In today's discussion, we are interested in dontdelete.

When declaring variables and functions, they become variable objects (Variable object)-Either the activation object (in the function code), or the properties of the global object (in global code), which are accompanied by the generation of internal property dontdelete. However, any explicitly/implicitly assigned property does not generate a dontdelete. And that's essentially why we can remove some of the attributes and not remove the other reason.

var global_object = this;

/* ' foo ' is a property of a global object that is generated by a variable declaration and therefore has internal properties Dontdelete

That's why it can't be deleted.

var foo = 1;
Delete foo; False
typeof Foo;//"Number"/
* ' Bar

' is a property of a global object that is generated by a variable declaration and therefore owns the Dontdelete child

That's why it can't be deleted too.

function bar () {};
Delete bar; False
typeof bar;//"function"

/* ' Baz ' is also a property of the global object,

However, it is generated by assigning values to properties, so there is no dontdelete

That's why it can be removed.

Global_object.baz = "Baz";
Delete Global_object.baz; True
typeof Global_object.baz;//"Undefined"

1.5. Built-in and Dontdelete | Build-ins and Dontdelete

So that's why all of this happens: a special internal property of a property controls whether the property can be deleted. Note: Some properties of the built-in object have internal property dontdelete and cannot be deleted, and special arguments variables (as we know, the properties of the activated object) have dontdelete; the length (return parameter lengths) property of any function instance has Dontdelete:

(function () {
  //Cannot delete ' arguments ' because there is dontdelete
  delete arguments;//false;
  typeof arguments; "Object"

  //cannot delete the length of the function because there is dontdelete function
  f () {};
  Delete f.length; false;
  typeof F.length; "Number"
}) ();

Properties 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, not declared variable assignment | Undeclared assignments

As you may recall, an undeclared variable assignment becomes a property of the global object, unless the property is found elsewhere in the scope chain. Now we understand the difference between attribute assignment and variable declaration-the latter generates Dontdelete and the former is not generated-which is why undeclared variable assignments can be deleted.

var global_object = this;

/* The properties of the global object are generated by variable declaration, owning Dontdelete
/var foo = 1;

/* To generate properties of global objects by assigning values to undeclared variables without dontdelete
/bar = 2;
Delete foo; False
Delete bar;//True

Note: Internal properties are determined at property generation, and subsequent assignment procedures do not alter the intrinsic properties of an existing property. It is important to understand this distinction.

* * * "foo" created while generating Dontdelete
/function foo () {};
/* After the assignment process does not change the intrinsic properties of existing attributes, Dontdelete still exists * *
foo = 1;
Delete foo; false;
typeof Foo; "Number"/
* But when a nonexistent property is assigned, a property without an intrinsic property is created, so there is no dontdelete
/this.bar = 1;
Delete bar; true;
typeof Bar; "Undefined"

Summarize:

Both variable and function declarations are properties of the activation (activation) Global object.

Property has internal properties, and one of the--dontdelete is responsible for determining whether a property can be deleted.

variables, function declarations in global Code or function code generate properties that have dontdelete.

Function arguments are also properties of the activated object and have dontdelete.

To delete a property in an object: Delete object. member

Can only delete its own members

Only global variables declared by Var do not let delete

Global members added with window. or window["" can delete

Ps:javascript the delete operator

Delete is one of the less frequently used operations in the JavaScript language, but sometimes it requires a delete operation when we need to do a delete or empty action. In this article, we'll delve into how to use it and how it works.

The purpose of deletion, as you might think, is to delete something, and more specifically, it deletes the properties of the object, as 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 will not delete the normal variable, as 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, in fact, properties of objects in the global object (the browser is window).

Because Var isn ' t used, this is a property of window
benjamin = "ZUOJJ";
Delete window.benjamin;
Referenceerror:benjamin is not defined
Console.log (Benjamin);

The delete operator also has a return value that returns True if a property is deleted, if the property cannot be deleted because the property is not writable, returns false, or if an error is thrown in strict mode.

var benjamin = {
  "name": "Zuojj",
  "url": "Http://www.zuojj.com"
};
var namedeleted = delete Benjamin.name;
Outputs:true
Console.log (namedeleted);
" Use strict ";
var benjamin_ = "Zuojj";
Outputs:uncaught Syntaxerror:delete of the unqualified identifier in strict mode.
Delete Benjamin_;

You may not know under what circumstances the deletion operator is used. The answer is, as long as you really want to remove an attribute from the object.

Sometimes, JavaScript development does not delete an attribute, but instead sets the property value to null. Like this:

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

Although this effectively cuts off properties from the original value, but the property itself still exists on the object, you can see the following:

outputs:object {name:null, url: "http://www.zuojj.com"}
Console.log (Benjamin);

Also, the presence of the null attribute will not be reported by the like in and for in loop operations, and if you use an object, you might use these methods to check an object, and you may want to make sure that you really delete any unwanted attributes.

Finally, you should remember that deletion does not break the value of the attribute, just the property itself, look at 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, and as you can see, deleting benjamin.name does not affect name.

Above, is my summary of the delete operator, the wrong place, you are welcome to criticize.

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.