1

I understand why hasOwnProperty is necessary even when one has complete control over an object, since Object.prototype may have been modified. But if I really want to avoid it, consider this:

function CleanObject() {
    var result = {};
    for (var key in result) {
        delete result[key];
    }
    return result;
}

// Later...

var obj = CleanObject();
for (var key in obj) {
    // No hasOwnProperty check necessary
}

In other words I'm clearing the instance of properties before using it. Should this work, or am I missing some edge-case?

DNS
  • 37,249
  • 18
  • 95
  • 132
  • http://es5.github.com/#x8.12.7 states: `delete`s do not propagate up the prototype chain (although not as clearly). – Dan D. Feb 23 '13 at 13:47

1 Answers1

1

You can also create a "Clean" object that has no properties using the Object.create() syntax passing in null as a param.

Example:

var clean = Object.create(null);

for(var key in clean) {
 //do stuff no hasOwnProperties required
}