Reputation: 25928
How do I remove all attributes from a Javascript object?
For example; if I have the following 'class' how can I perform a 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];
}
Upvotes: 6
Views: 3219
Reputation: 68152
Your code seems fine as is. Since delete
will not delete a property from the prototype, you do not even need to use hasOwnProperty
.
Upvotes: 6