What are some of the most detrimental Javascript tricks? Pleas include the “issue seen” and “avoid by” blocks.
Examples:
-
Adding properties to
Object.prototype.prop = 1
Issue seen:for(var i in obj){ alert(i);}
Avoid by: usinghasOwnProperty
example:
for(var in in obj)if(obj.hasOwnProperty(i)){alert(i);} -
Override
Number.prototype.valueOf = function(){return Math.random()}
Issue seen:4*3(Depends on Javascript engine)
Avoid by:delete Number.prototype.valueof(again depends on Javascript engine)
Please include potential solutions if you can’t think of a way to “avoid” the code.
Just read Crock’s appendix, “JavaScript, the bad parts”