This is why you should always check hasOwnProperty
:
for (var temp in tempObject) { if (Object.prototype.hasOwnProperty(tempObject, temp)) { console.log(temp); } }
Crockford supporters use Object.prototype.hasOwnProperty
instead of tempObject.hasOwnProperty
, just in case you override hasOwnProperty
in your object.
In ES5, you can set it not as enumerable
:
Object.defineProperty(Object.prototype, 'simpleFunction', { value: function() { return true; }, enumerable: false,
Alternatively (in ES5) you can use Object.keys()
to get only the object’s own keys:
Object.keys(tempObject).forEach(function(key) { console.log(key); });
source share