2014-12-03 105 views
1

在此之後代碼第一:爲什麼Object.keys(Array.prototype)返回空數組?

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); //[] - ??? 

如何解釋這種現象?

我們可以模仿我們嘗試創建的構造函數嗎?

+3

因爲它們是不可枚舉的屬性。 – elclanrs 2014-12-03 09:24:21

+1

Object.getOwnPropertyNames(Array.prototype) – havenchyk 2014-12-03 09:48:33

+0

關於時間有人回答了這個...... :) – deostroll 2014-12-04 04:03:26

回答

0

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 loops露面。

相關問題