Remove Attributes from Javascript Object

How to remove all attributes from a Javascript object?

For instance; if I have the following “class”, how can I reset and remove all its attributes:

function MyObject()
{
   this.type="blah";
   this.name="kkjkj";
}

MyObject.prototype.clearAttribs = function()
{
   // I want to remove name, type etc from 'this'

   // Maybe I can do the following?
   for (var key in this)
      delete this[key];
}
+3
source share
1 answer

Your code seems beautiful. Since it deletedoes not remove the property from the prototype, you do not even need to use it hasOwnProperty.

+3
source

All Articles