2012-09-01 104 views
4

在Chrome中,函數Empty(){}在哪裏?

(function(){}).__proto__ 

function Empty() {} 

,所以我本來期望

new Function(); 

function Empty() {},而是它是function anonymous() {}

我在哪裏可以找到function Empty() {}?它在Function還是Object某處?

回答

6

這些名稱並不意味着您可以使用這些標識符訪問它們。

至於功能的原型,其名稱是set at startup。該功能已被設置爲名稱,但除了.name.toString()外,它沒有任何用途。

至於new Function()實例,名稱僅爲printed for .toString()。所以.name仍然是空字符串。再次,它沒有太多的目的。

6

空函數對象(名稱爲Empty)是Function的原型。

Function.prototype.toString() === "function Empty() {}" 

Object.getPrototypeOf(new Function()).toString() === "function Empty() {}" 
+0

謝謝。那麼函數anonymous(){}'呢? – Randomblue

+0

@Randomblue'new Function()。toString()'會給你。 – xdazz

2

Empty是Function的原型的名稱。從Chrome控制檯:

dir(Function) 
    function Function() { [native code] } 
    arguments: null 
    caller: null 
    length: 1 
    name: "Function" 
    prototype: function Empty() {} 
    __proto__: function Empty() {} 
    apply: function apply() { [native code] } 
    arguments: null 
    bind: function bind() { [native code] } 
    call: function call() { [native code] } 
    caller: null 
    constructor: function Function() { [native code] } 
    length: 0 
    name: "Empty" 
    toString: function toString() { [native code] } 
    __proto__: Object