我有一个库,其中添加了以下数组类型 -
Array.prototype.top = function() {
return (this.length > 0) ? this[this.length-1] : null;
}
它似乎适用于非数组对象 - 例如,
for (key in myrecordset) {
alert(key); // will iterate and alert "top";
}
在调试器控制台中:
> myrecordset.length
< undefined
> typeof myrecordset
< 'object'
> myrecordset instanceof(Array)
< false
> myrecordset['top']
< f() {
return (this.length ...
}
那么,这是什么情况?该对象不是 JavaScript 数组(即,没有长度,不是实例,...),但 Array.prototype.top 似乎已应用?
注意:此外,尝试遵循原型链我得到
> myrecordset.constructor()
< {}
[[ Prototype ]]: Object
对象可以通过多种方式获得等于的属性
top
。屏幕截图证明,myrecordset
具有的top
属性不是继承的属性,而是“自身”属性。这意味着它被复制到那里。以下是可能发生的一种情况:
可能防止这种情况的一种方法是在 Array 原型上定义
top
为不可枚举的属性:为了正确扩展原型,您应该使新 props 不可枚举。对于您的情况,您可能对在包含有问题的代码后运行的修复感兴趣,它会将所有可枚举的 props 转换为不可枚举的: