Interestingle when I add a method to the Object
and loop through my object, the method which is added to the prototype becomes one of the keys. Please see the following example:
var a = {"foo":"bar"};
Object.prototype.clean= function() {
}
for(var i in a){
console.log("i:", i);// logs foo,clean
}
So thats a problem; I dont want this behaviour; I came up with the following workaround:
var a = {"foo":"bar"};
Object.prototype.clean= function() {
}
var keys= Object.keys(a);
for(var i=0; i < keys.length; i++){
console.log("i", i); //now it just logs foo
}
But since I have lots of object in my project I dont want to change the behaviour of my simple loop for(var i in a){
. Does that mean I should not add my method to the prototype object in order not to see these weird behaviour? and I should have it as a simple function as opposed to adding the Object prototype?