Слово предупреждения при работе с прототипом и типами данных Object, если вы используете цикл for
, полная функция возвращается как одна из пар ключ / значение. См. Основные примеры ниже и комментарии.
// Basic hash-like Object
var test = {
'a':1,
'b':2,
'c':3,
'd':4
};
// Incorrect
// badAlerter prototype for Objects
// The last two alerts should show the custom Object prototypes
Object.prototype.badAlerter = function() {
alert('Starting badAlerter');
for (var k in this) {
alert(k +' = '+ this[k]);
}
};
// Correct
// goodAlerter prototype for Objects
// This will skip functions stuffed into the Object.
Object.prototype.goodAlerter = function() {
alert('Starting goodAlerter');
for (var k in this) {
if (typeof this[k] == 'function') continue;
alert(k +' = '+ this[k])
}
};
test.badAlerter();
test.goodAlerter();