2012-06-03 97 views
3

在編寫JavaScript時,我經常忘記一些內置對象的屬性,並且必須在mdn中查找它們,這很煩人,因爲它會減慢我的工作。任何方式來遍歷所有的非枚舉屬性?

而不是參考文檔,創建一個對象並使用for ... in檢查console.log()更方便。但是當涉及到非枚舉屬性時,即使for ... in也無濟於事。

所以我的問題是,除了谷歌和文件,有沒有辦法檢查非枚舉屬性?

for(var i in Object){ 
    console.log([i,Object[i]]); 
    // ["wtbind", function()] 
} 
console.log(Object.hasOwnProperty('create')); 
// true 
// Here Object.create is a non–enumerable property, 
// and I have to look it up in documents if I forget it. 
+0

你用什麼瀏覽器進行開發?你有沒有考慮'console.dir()'而不是'console.log()'? – lanzz

+0

不可枚舉是否禁用使用Object.toSource();的能力? – Steve

+0

@lanzz firefox和螢火蟲。我不知道有一個'console.dir()'。我試過'console.dir(Object); //顯示「prototype Object {}」',但那些不可枚舉的屬性如Object.create沒有顯示出來。 – Rufus

回答

3

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames

似乎是有用的,雖然它是一個相當最近增加,顯然在Opera不起作用。雖然它只列出自己的屬性,但您始終可以爬上原型鏈。

+0

Object.getOwnPropertyNames(Object); // [「prototype」,「getPrototypeOf」,「getOwnPropertyDescriptor」,「keys」,「defineProperty」,「defineProperties」,「create」,「getOwnPropertyNames」,「isExtensible」,「preventExtensions 「,」freeze「,」isFrozen「,」seal「,」isSealed「,」length「,」name「,」arguments「,」caller「],太好了!如果我想知道,'for'循環會顯示它們的類型。 – Rufus

+0

而且因爲我只會在開發環境中使用'Object.getOwnPropertyNames',例如。 firefox和nodeJS,Opera的問題對我來說可以。再次感謝你。 – Rufus

+0

@Rufus Ah,那麼OK :)另一方面,我主要使用Opera,所以我不高興地發現這一點;) – Imp