2012-09-12 33 views
9

我想要包裝的數組對象所有陣列的功能,但在控制檯我如何可以遍歷Array.prototype功能

>>> Array.prototype 
[] 
>>> [].prototype 
undefined 

但是當我在控制檯輸入Array.prototype它顯示autocomple的所有功能,我怎麼能獲得這些功能?他們隱藏在哪裏?

回答

16

你的意思是:

var arrObj = Object.getOwnPropertyNames(Array.prototype); 
for(var funcKey in arrObj) { 
    console.log(arrObj[funcKey]); 
} 
+0

是的,正是我在尋找的東西。謝謝。 – jcubic

+0

不客氣... :) –

+0

IE8-的任何墊片? – David

0
var proto = Array.prototype; 

for (var key in proto) { 
    if (proto.hasOwnProperty(key)) { 
     console.log(key + ' : ' + proto[key]); 
    } 
} 

demo.

如果你要檢查它的控制檯屬性。

用途:console.dir(Array.prototype);

+0

這別工作。 (在谷歌瀏覽器中),正如我提到的那樣,Array.prototype在以這種方式訪問​​它時是空數組。 – jcubic

+0

@jcubic它應該可以工作,你可以檢查演示。 – xdazz

+0

它不,所以我問這個問題。 'console.dir(Array.prototype);'用這些函數返回數組[0],但迭代Array.prototype不起作用。 – jcubic

1

使用ECMAScript 6(ECMAScript的2015年),可以簡化一下:

for (let propName of Object.getOwnPropertyNames(Array.prototype)) { 
    console.log(Array.prototype[propName]); 
}