We have some questions here that discuss delete in a more abstracted way, but I'm looking for practical examples of when use of delete
could be used, versus doing something such as setting the property to null or undefined.
The delete operator deletes a property of an object.
What's a case of a challenge faced somewhere, that 开发者_StackOverflow中文版delete
was the best solution, versus something else?
When using an object as a hashmap, you can iterate over the object's properties using:
for (var key in obj) {
// ...
}
If some properties of that objects were set to null
, their keys would be included there. By using delete
you can remove their keys completely.
Object.defineProperty(Object.prototype, "Incognito", {
get: function() { return 42; },
set: function() { },
configurable: true
});
console.log(({}).Incognito); // 42
({}).Incognito = null;
console.log(({}).Incognito); // 42
// I DO NOT WANT INCOGNITO
delete Object.prototype.Incognito
console.log(({}).Incognito); // undefined
Any property which has an empty setter (Because someone thought that was a good idea) needs to be deleted if you want to get rid of it.
var hash = {
"FALSE": undefined,
"TRUE": null
}
console.log("TRUE" in hash); // true
console.log("FALSE" in hash); // true
delete hash.FALSE;
console.log("FALSE" in hash); // false
The in
operator returns true
for any property that exist no matter it's value. If you want it to return false
you need to delete
the property.
In both these cases setting it to null
or undefined
does nothing (Because it either has a setter that does nothing, or that's how the in
operator works)
Setting an object's property to null
or undefined
will still leave the property as enumerable - if you're going to do a for..in
over the object at any stage and the presence of a property is significant, this is when you'd want to delete
instead.
For example, if you have one constructor which takes arguments as an object, which inherits from another constructor which does the same and the presence of a property is significant when calling the parent constructor (say, in the example below, that ParentWidgets used its arguments with a for..in
to generate HTML attributes) you would want to use delete
to remove properties which aren't relevant to the parent:
function ChildWidget(kwargs) {
kwargs = extend({
childSpecific1: null, childSpecific2: 42
}, kwargs || {})
this.childSpecific1 = kwargs.childSpecific1
this.childSpecific2 = kwargs.childSpecific2
delete kwargs.childSpecific1
delete kwargs.childSpecific2
ParentWidget.call(this, kwargs)
}
inherits(ChildWidget, ParentWidget)
The delete
operator is useful in a reset or clear method for removing object literal data tied to forms:
delete formmap["forms"]
It is also useful for deleting objects tied to state:
/* lights... */
if (node["alpha"+i].active)
{
// camera, action
node["beta"+i] = chi;
}
else
{
/* cut! */
delete node["beta"+i];
node["omega"].state = false;
}
In addition, it's useful as a shorthand for inlining optional object properties:
var foo = {"bar": [], "drink": [], "tab": [] }
// happy hour
this.bar && (foo["bar"]).push(this.bar) || delete foo.bar;
// open a tab
this.drink && (foo["drink"]).push(this.drink) || delete foo.drink;
// cheers
this.tab && (foo["tab"]).push(this.tab) || delete foo.tab;
Finally, it's useful as a way to distinguish between types by using the writable permissions of type specific instance properties as a litmus test:
// Function
!!foo.prototype === true && delete foo.length === false && delete foo[-1] === true
// Object, Number, Infinity, or Boolean (probably Object)
!!foo.prototype === false && delete foo.length === true && delete foo[-1] === true
// Array
!!foo.prototype === false && delete foo.length === false && delete foo[-1] === true
// String
!!foo.prototype === false && delete foo.length === false && delete foo[-1] === false
// RegExp
delete foo.source === false
References
Adobe Flash Platform: delete statement
JScript Reference: Delete Operator
精彩评论