The Delete in JavaScript

Source: Internet
Author: User

So, why can we delete the properties of an object:

var x = {a:1};d elete x.a; TRUEX.A; Undefined

But you cannot delete a variable:

var x = 1;delete x; False;x; 1

You cannot delete a function:

function X () {};d elete x; False;typeof x; "Function"
    • Note: Delete returns false only if a property cannot be deleted .
    • Each property has 0 to many, such as internal properties--*readonly,dontenum,dontdelete and internal**. You can think of them as labels--a property may or may not have a particular intrinsic property. In today's discussion, we are interested in dontdelete.
    • When variables and functions are declared, they become variable objects (Variable object)-Either the active object (in the function code) or the Global object (in the global Code)-the properties that accompany the internal property Dontdelete generated. However, any explicit/implicit assignment of properties does not generate Dontdelete. And that's essentially why we can delete some properties and not remove other reasons.
var GLOBAL_OBJECT = this;/* ‘foo‘是全局对象的一个属性,    它通过变量声明而生成,因此拥有内部属性DontDelete    这就是为什么它不能被删除*/var foo = 1;delete foo; // falsetypeof foo; // "number"/* ‘bar‘是全局对象的一个属性,    它通过变量声明而生成,因此拥有DontDelete子    这就是为什么它同样不能被删除*/function bar() {};delete bar; // falsetypeof bar; // "function"/* ‘baz‘也是全局对象的一个属性,    然而,它通过属性赋值而生成,因此没有DontDelete    这就是为什么它可以被删除*/GLOBAL_OBJECT.baz = "baz";delete GLOBAL_OBJECT.baz; // truetypeof GLOBAL_OBJECT.baz; // "undefined"

1.5, built-in and Dontdelete | Build-ins and Dontdelete

So that's why all this happens: a special intrinsic property of the property controls whether the property can be deleted. Note: Some properties of the built-in object have intrinsic properties dontdelete, so they cannot be deleted; Special arguments variables (as we know, properties of activated objects) have dontdelete; the length (return parameter length) property of any function instance also 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, undeclared variable assignment | Undeclared assignments
    • As you may recall, an undeclared variable assignment becomes a property of the global object unless it is found elsewhere within 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 the undeclared variable assignment can be removed.
var GLOBAL_OBJECT = this;/* 通过变量声明生成全局对象的属性,拥有DontDelete */var foo = 1;/* 通过未声明的变量赋值生成全局对象的属性,没有DontDelete */bar = 2;delete foo; // falsedelete bar; // true

Note: Internal properties are determined when the property is generated, and subsequent assignments do not change the intrinsic properties of an existing property. It is important to understand this distinction.

/* ‘foo‘创建的同时生成DontDelete */function foo() {};/* 之后的赋值过程不改变已有属性的内部属性,DontDelete仍然存在 */foo = 1;delete foo; // false;typeof foo; // "number"/* 但赋值一个不存在的属性时,创建了一个没有内部属性的属性,因此没有DontDelete */this.bar = 1;delete bar; // true;typeof bar; // "undefined"


总结:
  • Both variable and function declarations are properties of the active (Activation) Global object.
  • The--dontdelete property has internal properties, one of which is responsible for determining whether a property can be deleted.
  • variables, function declarations in global Code or function code generate properties that have dontdelete.
  • The function parameters are also properties of the activated object and have dontdelete.
  • Delete the properties in the object:Delete object . member

    You can only delete your own members

    only var the declared global variable does not let Delete

    Use window. or window[""] an increased global member can Delete

The Delete in JavaScript

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.