HomeBlog
Delete As In JavaScript : JavaScript Language Feature - 羡辙杂俎
2015.03.03 20:16:04
Delete As In JavaScript

Series: JavaScript Language Feature

The delete operator removes a property from an object. Its single operand should be a property access expression.

JavaScript: The Definitive Guide, 6th Edition

delete is used to remove a properpty from an object.

var a = {x: 2, y: 3};
delete a.x; // Returns true; `a` is changed to be {y: 3}

But there are circumstances that we may expect the wrong result from it. Here’s what JavaScript: The Definitive Guide tells us.

A delete expression evaluates to true if the delete succeeded or if the delete had no effect (such as deleting a nonexistent property). delete also evaluates to true when used (meaninglessly) with an expression that is not a property access expression:

o = {x:1};         // o has own property x and inherits property toString

delete o.x;        // Delete x, and return true

delete o.x;        // Do nothing (x doesn't exist), and return true

delete o.toString; // Do nothing (toString isn't an own property), return true

delete 1;          // Nonsense, but evaluates to true

delete a.x;        // returns true and `a` is changed to be {y: 3}

delete does not remove properties that have a configurable attribute of false. (Though it will remove configurable properties of nonextensible objects.) Certain properties of built-in objects are nonconfigurable, as are properties of the global object created by variable declaration and function declaration. In strict mode, attempting to delete a nonconfigurable property causes a TypeError. In non-strict mode (and in ECMAScript 3), delete simply evaluates to false in this case:

delete Object.prototype; // Can't delete; property is non-configurable

var x = 1;               // Declare a global variable

delete this.x;           // Can't delete this property

function f() {}          // Declare a global function

delete this.f;           // Can't delete this property either

JavaScript: The Definitive Guide, 6th Edition

As mentioned several times above, what is configurable?

The configurable attribute controls at the same time whether the property can be deleted from the object and whether its attributes (other than writable) can be changed.

Object.defineProperty() - JavaScript | MDN

Be careful when deleting on global objects. Here are some interesting results that may be confusing.

// In global scope

var x = 1;  delete this.x; // Returns false;

y = 1;      delete this.y; // Returns true;

this.z = 1; delete this.z; // Returns true;


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

y = 1;      delete y;      // Returns true;

this.z = 1; delete z;      // Returns true;

Why? How is x different from y in this case?

We know that y, an undeclared variable can be used in almost the same way like declared variables like x. Then, how are they different in this case?

Matt Coughlin gives a very clear answer to this question, which I’d suggest reading. To be brief, for declared global variables, the configurable attribute is false. For undeclared global variables, it’s true. And properties can be deleted only when it’s configurable attribute is true.

博文对你有帮助吗?如果有的话,要不要送我一本书呢?:heart_eyes:

Series: JavaScript Language Feature


微信公众号“羡辙部落格”(xianzheblog)提供本站优质非技术类博文推送。

Creative Commons License
Every blog post in this site by Wenli Zhang is licensed under a CC 3.0 License, based on a work at Github. You can see the source code of this blog site at github.com/Ovilia/blog.

Comments

comments powered by Disqus