delete in js

The delete operator removes a property from an object.

delete is only effective on an object’s properties. It has no effect on variable or function names.

If the object inherits a property from a prototype, and doesn’t have the property itself, the property can’t be deleted by referencing the object. You can, however, delete it directly on the prototype.

function Foo(){}
Foo.prototype.bar = 42;
var foo = new Foo();

// returns true, but with no effect, 
// since bar is an inherited property
delete foo.bar;           

// logs 42, property still inherited
console.log(foo.bar);

// deletes property on prototype
delete Foo.prototype.bar; 

// logs "undefined", property no longer inherited
console.log(foo.bar);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s