[英]Why passing function object to Object.keys() returns empty array?
[英]why Object.keys(Array.prototype) returns empty array?
首先遵循以下代碼:
function User(name, dept){
this.username = name;
this.dept = dept;
this.talk = function(){
return "Hi";
};
}
function Employee(){
User.apply(this, Array.prototype.slice.call(arguments));
}
Employee.prototype = new User();
for(var x in Employee.prototype)
console.log(x, ':', Employee.prototype[x]);
Object.keys(Employee.prototype);//prints array of keys...
打印得很好...
Array.prototype; //[]
Array.prototype.slice; // Function
var o = Array.prototype;
for(var i in o) console.log(i, ':', o[i]); //it doesn't execute
Object.keys(Array.prototype); //[] - ???
如何解釋這種行為?
我們可以為嘗試創建的構造函數模仿嗎?
Object.keys()- MDN
Object.keys()
方法返回給定對象自己的可枚舉屬性的數組...
我們可以檢查給定的屬性是否可枚舉:
Array.prototype.propertyIsEnumerable('push');
// false
另外,我們可以獲取對象屬性的完整描述符,其中也包括可枚舉標志。
Object.getOwnPropertyDescriptor(Array.prototype, 'push');
// {writeable: true, enumerable: false, configurable: true}
Array.prototype上的屬性是故意不可枚舉的,因此它們不會出現在for...in
循環中 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.