I want to test whether an object is empty: {}
. The following is typically used:
function isEmpty(obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop))
return false;
}
return true;
}
But suppose the Object
prototype was added to as follows:
Object.prototype.Foo = "bar";
Tests:
alert(isEmpty({})); // true
Object.prototype.Foo = "bar";
alert({}.Foo); // "bar" oh no...
alert(isEmpty({})); // true ...**huh?!**
I tried to nuke the object's prototype, change it's constructor, and all manner of such hacks. Nothing worked, but maybe I did it wrong (probable).