1

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?

user385729
  • 1,924
  • 9
  • 29
  • 42
  • Use `if( a.hasOwnProperty(i) ) { ... }` with `for in` loops to avoid this problem. – Andy Ray Sep 22 '15 at 20:26
  • I already know the workaround to just get the real keywords of my object; but since I have lots of objects and lots of loops I dont want to go through each loop and change the syntax just for my added prototype – user385729 Sep 22 '15 at 20:33
  • If you are going to loop over non-vanilla objects with `for in` that is the correct paradigm, suggested in the book "Javascript: The Good Parts". Not wanting to do more work isn't relevant ;) – Andy Ray Sep 22 '15 at 21:23

2 Answers2

0

Please, use the defineProperty method to declare the Object.prototype.clean with enumerable:false descriptor option. It will prevent your new method from iteration.

Object.defineProperty(Object.prototype,'clean',{enumerable:false,value:function(){
  //FUNCTION CLEAN BODY
}})
Eugene Tiurin
  • 4,019
  • 4
  • 33
  • 32
0

You should never extend the prototype of built-in objects (like Object, String, Array), because you cannot be sure that another library breaks because of your extension. And to be sure to handle this kind of trap, iterating over objects should check against hasOwnProperty.

Object.keys() creates an array of the own properties, so this is safe against such extensions.

When using jshint it would tell you what is wrong with the first loop.

hgoebl
  • 12,637
  • 9
  • 49
  • 72
  • So does that mean I should have my standalone simple function instead of adding to the prototype of object? – user385729 Sep 22 '15 at 20:35
  • @Pasargad You realize that adding to Object's prototype is actually applicable to all the objects (Functions, Arrays, Numbers, Strings, etc), right? Do you really want that? The function you add is common to all of them? – thefourtheye Sep 22 '15 at 20:37
  • Ummm added prototype to Object affects Number and String too? Seriously? Anyway No definitely I need it just for object even not Array. So does that mean I need to have my own standalone simple function? – user385729 Sep 22 '15 at 20:41
  • Please just don't do it. The only valid case is to provide methods which are present in newer versions of JavaScript. These libs are called "shim" or "shiv". Can you image what happens when everybody extends prototypes of built-in types? – hgoebl Sep 23 '15 at 06:17