2016-04-28 191 views
1

我正在運行三段代碼。第一個工作,而第二個和第三個不起作用。我不知道我明白爲什麼第二和第三不起作用。對象內部對象的範圍在對象內的函數內

user2 = { 
      handle :"mytext", 
      alertName: function(){ 
        alert(this.handle);} 
     }; 
user2.handle; //"mytext" 
user2.alertName() // alerts "mytext" 

在這裏,我明白句柄分配給user2對象,並且是user2對象上的鍵。因此,當我調用user2.alertName()時,this.handle引用user2.handle,因此打印出正確的值。

但是,爲什麼不將下面的代碼工作的兩個版本:

user2 = { 
      handle :"mytext", 
      alertName: function(){ 
        alert(handle);} 
     }; 

user2.handle; //"mytext" 
user2.alertName() // gives error that handle is not defined in the console 

user2 = { 
      handle :"mytext", 
      alertName: function(handle){ 
        alert(handle);} 
     }; 

user2.handle; //"mytext" 
user2.alertName() //gives undefined in the alert popup 

據我瞭解的範圍,處理其在user2的對象被定義應該可用作爲user2的子範圍的函數。 我在這裏錯過了一些基本的東西。如果這看起來像一個noob問題,但我認爲我知道了,並總是不斷回來。我無法在任何地方得到明確的答案,因爲當我查找文檔時爲什麼第二個或第三個沒有意義。

+0

感謝,並在這裏,我向一個重要的點。使用對象名稱和'this'是不一樣的。我查了下面的答案,以瞭解不同之處http://stackoverflow.com/questions/25226693/whats-the-difference-between-using-this-and-the-object-name-for-reference-insi。希望它也能幫助其他人 –

回答

1
user2 = { 
      handle :"mytext", 
      alertName: function(){ 
        alert(handle);} 
     }; 

user2.handle; //"mytext" 
user2.alertName() // gives error that handle is not defined in the console 

handleobjectproperty,所以你需要通過this它指的是object訪問對象的屬性。

alert(handle);拋出錯誤coz它試圖找到一個名爲handle的變量不在範圍內。

如果您定義了varibale句柄globally,那麼您可以在您的user2object中訪問它。

var handle="google"; 

user2 = { 
      handle :"mytext", 
      alertName: function(){ 
        alert(handle);} 
     }; 

alert(user2.handle); //"mytext" 
user2.alertName() // gives error that handle is not defined in the console 
2

在你的第二個和第三個代碼:

user2 = { 
      handle :"mytext", // handle is a property 
      alertName: function(){ 
        alert(handle);} 
     }; 
  1. 「把手」 是對象用戶2的屬性。
  2. 這個是你正在調用的方法的對象。
  3. 「句柄」屬於這個屬性的方法「alertName」。

因爲,句柄不是定義的變量,所以錯誤。 在第三種情況下,您將方法定義爲方法簽名中的參數,但不會在調用中傳遞任何參數,因此將其定義爲未定義。

0
user2 = { 
      handle :"mytext", 
      alertName: function(){ 
        alert(this.handle);} 
     }; 

user2.handle; //"mytext" 
user2.alertName() // alerts "mytext" 

句柄是對象的屬性,所以你需要通過這個它是指對象來訪問對象的屬性。

1

在第二個示例中,函數'alertName'需要一個參數'handle'。 調用沒有參數的user2.alertName()結果未定義。

對於作品應該是這樣的:

user2.alertName(user2.handle); 
+0

這很有趣。所以'user2.handle'仍然可用。當對象 - user2或這個 - 沒有明確提及時,句柄是否變得模糊不清? –

1

我將與代碼示例,隨後的解釋開始。

var user2 = { 
    handle :"mytext", 
    alertName: function() { 
    //var handle is not defined in the current lexical scope or globally. 
    alert(handle); 
    } 
}; 

user2.handle; //"mytext" 
user2.alertName() // gives error that handle is not defined in the console 

說明: 要定義user2.alertName。在這個功能中,您打電話給alert(handle)。從我所看到的handle未在當前詞彙範圍內定義,或全局,因此undefined被警告。

var user2 = { 
    handle :"mytext", 
    alertName: function(handle){ 
    alert(handle); 
    } 
}; 

user2.handle; //"mytext" 
user2.alertName() //gives undefined in the alert popup 

說明: 你定義user2.alertName。該功能需要一個提醒的參數。 當你調用user2.alertName()時,你沒有傳遞任何變量或文字。 因此,undefined被警告。 提醒user2.handle使用:

user2.alertName(user2.handle); 

我希望這有助於

里斯的答案