2014-01-16 246 views
0

我以爲我明白JavaScript範圍如何工作到現在。這是我JavaScript範圍問題

function MFCommentsHandler(node, type, item) { 
    this.get = function(returnType){ 
     ... 
    } 

    function getButton(){ 
     var button = document.createElement('button'), 
      get = this.get; 
     button.innerHTML = 'Load more comments'; 
     button.onclick = function(){ 
      console.log(get); 
      get('html'); 
     } 
     return button; 
    } 

} 

我使用內部函數通過getButton生成的文檔元素追加,但是當我點擊它,我得到get is not a function。爲什麼getonclick函數裏面沒有定義?

+0

您是否正在全球化? –

+0

,因爲'this'是你調用'MFCommentsHandler'的任何上下文。除非你使用'new'構造一個新對象。 –

+0

在你的情況下,我只是使用var get = function ... –

回答

2

this僅取決於如何調用函數。它可以是隱含的,但在你的情況下,你失去了上下文。你需要在上範圍緩存this

function MFCommentsHandler(node, type, item) { 
    var self = this; 
    this.get = function(returnType){ 
     ... 
    } 
    function getButton(){ 
     self.get(); // cached reference 
     ... 
    } 
} 

或者當你打電話getButton你可以明確地通過上下文:

function MFCommentsHandler(node, type, item) { 
    this.get = function(returnType){ 
     ... 
    } 
    function getButton(){ 
     this.get(); 
     ... 
    } 
    getButton.call(this); // explicit context 
    ... 
+0

啊..這麼簡單的錯誤..謝謝先生。 –

+0

「*這隻取決於你如何調用函數*」。究竟。 「*但在你的情況下,你失去了上下文*」。這與第一個陳述有衝突。你的意思是*這個*在呼叫中沒有設置爲所需的值。 – RobG

+0

我的意思是'this'不是以前所以「你失去了語境」,但從技術上來說,是的,這是因爲'this'沒有在通話中設置。但是這個調用可以是隱式的,就像在'obj.method()'中那樣,但是你可以在沒有點符號的情況下調用這個函數,這樣你就失去了上下文。我就是這樣解釋的。 – elclanrs

2

this變化與上下文被引用的。試試這個:

function MFCommentsHandler(node, type, item) { 
    var get = function(returnType){ 
     ... 
    } 

    function getButton(){ 
     var button = document.createElement('button'); 
     button.innerHTML = 'Load more comments'; 
     button.onclick = function(){ 
      console.log(get); 
      get('html'); 
     } 
     return button; 
    } 

} 
+1

但我不能從外面呼叫'get' –

+0

@php_nub_qq這是真的。在這種情況下,請與elclanrs的回答一起。 – Matt

1

你所定義的MFCommentsHandler()函數中定義的getButton()功能是關閉。閉包有權訪問其外部函數的變量和參數(但不包括對象)。但是,他們無法訪問其外部功能的值。要訪問此值,只需將其引用存儲在變量中,以便可以從內部函數訪問它。

function MFCommentsHandler(node, type, item) { 
    var that = this; //reference to the invocation context 
    //that can be accessed anywhere within this function 

    this.get = function(returnType){ 
     ... 
    } 

    function getButton(){ 
     ... 
     that.get('html'); 
    } 

}