Friday, 10 June 2011

prototype js breaks javascript for in loops

The for in statement allows for looping through the names of all of the properties of an object. Unfortunately, it also loops through all of the properties that were inherited through the prototype chain. This has the bad side effect of serving up method functions when the interest is in data properties. If a program is written without awareness of this situation, then it can fail. So always wrap the body of a for loop inside an if statement by adding a hasOwnProperty condition.

Reference:
http://www.jslint.com/lint.html
http://zurahn.wordpress.com/2009/01/11/prototypejs-breaks-javascript-foreach-loops/

No comments:

Post a Comment