Does JavaScript's for in loop iterate over methods?
Posted
by hekevintran
on Stack Overflow
See other posts from Stack Overflow
or by hekevintran
Published on 2010-03-30T01:40:58Z
Indexed on
2010/03/30
1:43 UTC
Read the original article
Hit count: 674
JavaScript
|for-in-loop
In an article on yuiblog Douglas Crockford says that the for in
statement will iterate over the methods of an object. Why does the following code not produce ["a", "b", "c", "d", "toString"]? Aren't .toString() and other methods members of my_obj?
Object.prototype.toString = function(){return 'abc'}
Object.prototype.d = 4;
my_obj = {
'a':1,
'b':2,
'c':3
}
a = []
for (var key in my_obj) {
a.push(key)
}
console.log(a) // prints ["a", "b", "c", "d"]
© Stack Overflow or respective owner