You should be aware that JavaScript has a prototype inheritance model instead of classic. In fact, this means that inheritance is done through so-called prototype chains.
When you try to access the properties of an array using "for in", it will go up the prototype chain up to arrayname.prototype, since it inherits it!
, JavaScript Garden Ivo Wetzel, !
, javascript , .: First Property - '0', Second - '1' ..
, :
test("hasOwnProperty", function() {
var keys = [];
var fruits = ['apple', 'orange'];
for(propertyName in fruits) {
keys.push(propertyName);
}
ok(keys.equalTo(['0', '1', 'fruits.prototype'), 'what are the properties of the array?');
var ownKeys = [];
for(propertyName in fruits) {
if (fruits.hasOwnProperty(propertyName)) {
ownKeys.push(propertyName);
}
}
ok(ownKeys.equalTo(['0', '1']), 'what are the own properties of the array?');
});