Beware of properties inherited from the object's prototype (which could happen if you're including any libraries on your page, such as older versions of Prototype).
(注意从对象原型继承的属性(如果您在页面上包含任何库,可能会发生这种情况,例如旧版本的Prototype)。)
You can check for this by using the object's hasOwnProperty()
method.(您可以使用对象的hasOwnProperty()
方法来检查这一点。)
This is generally a good idea when using for...in
loops:(当使用for...in
循环时,这通常是一个好主意:)
var user = {};
function setUsers(data) {
for (var k in data) {
if (data.hasOwnProperty(k)) {
user[k] = data[k];
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…