2016-07-03 60 views
1
var hello = { 
    name: "Vishal", 
    speak: function(to){ 
     return function(){ 
      console.log(this.name+" says hello "+to); 
     }(); 
    } 
} 

我調用該函數訪問 -如何使名稱在此代碼

hello.speak("Vinay"); 

實際產量

says hello to Vinay

預計產量

Vishal says hello to Vinay

我知道hello.name將解決此問題,但如何解決它使用this,以便使用call方法或applybind方法可以解決這個問題。

+3

爲什麼你要返回另一個函數的結果,而不是直接在'speak'中調用'console.log'?它會在這種情況下工作。問題在於內部函數是用不同的上下文('this')調用的。 –

+0

@ user3666112使用hello.name而不是 –

+0

我只是想了解使用調用,這是我的要求,而不是console.log你可以考慮另一個返回這裏。 – user3666112

回答

2

什麼是你的代碼發生的事情:

您已經綁定了一個對象hello,該函數返回另一個輸出內容的函數。外部函數充當內部函數的閉包。 由於編譯代碼的方式,JavaScript的行爲有點不同。內部函數不會識別這個變量,而是選擇全局的this.name

要解決此問題,您將不得不這樣做:

var hello = { 
    name: "Vishal", 
    speak: function(to){ 

    //we tell this closure that this is to be taken from function scope and no the global scope 
     var self = this; 
     return function(){ 
      console.log(self.name+" says hello "+to); 
     }(); 
    } 
} 

hello.speak("Vinay"); 

瞭解使用情況下綁定

綁定:

創建函數的副本結合被稱爲。然後,您可以傳遞要與此關鍵字關聯的對象或範圍。

例子:

var hello = { 
    name: "Vishal", 
    speak: function(to){ 
     return function(){ 
      console.log(this.name+" says hello "+to); 
     }; 
    } 
} 

var speakTo = hello.speak("Vinay"); 


var speakToCall = speakTo.bind(hello); //will give you the desired output. 

speakToCall(); 

現在,這是不綁定的實際使用情況,打電話或應用。這只是向您展示如何使用綁定來實現您的功能。

實際使用情況可以是這樣的:

使用案例:

When you have multiple objects like: 

var a = { 
    firstname: "rahul", 
    lastname: "arora", 
    getFullName: function(){ 
     return this.firstname + ' ' + this.lastname; 
    } 
} 

//Another object with same properties but without the function 
var b = { 
    firstname: "Micheal", 
    lastname: "Angelo", 
} 

//Rather than defining that function again in the object 'b' you can use bind, call or apply to get the desired output. 

console.log(a.getFullName.call(b)); // will output Micheal Angelo which is associated to b 

我希望它能幫助。

+0

感謝@Rahul,這是有效的,我理解這一點會探索更多的理解它。 – user3666112

+0

或者:'return function(){...} .bind(this);'保存來自用戶的'.bind()'調用。 –

+0

謝謝@Rahul你對我理解這件事大有幫助,我正在嘗試你向我明確表達的錯誤工作。非常感謝先生.. – user3666112

4

hello本身是可以訪問的。

var hello = { 
 
    name: "Vishal", 
 
    speak: function(to){ 
 
     return function(){ 
 
      console.log(hello.name+" says hello "+to); 
 
     }(); 
 
    } 
 
} 
 

 
hello.speak("Vinay");

+0

感謝您的回覆,但朋友,我的要求是使它可以通過調用,綁定或應用方法以某種方式訪問​​,並將this.name保留在那裏。 – user3666112

+0

不知道你好是否可以訪問本身是正確的術語,雖然...它是可訪問的,因爲它是全球變量 – Nishant

0

當你做hello.speak,則this將成爲hello對象。因此this.name已經是Vishal

var hello = { 
    name: "Vishal", 
    speak: function(to){console.log(this.name + " says hello " + to)} 
    } 
} 


hello.speak("Vinay"); 

如果你真的想這樣做,你的問題問的方式,你可以這樣做:

var hello = { 
    name: "Vishal", 
    speak: function(to){ 
     var self = this; 
     return function(self){ 
      console.log(self.name + " says hello to " + to); 
     }(self); 
    } 
} 

您也可以跳過傳遞self因爲lexical作用域允許你訪問了這一點。

但照片你在做什麼。你有一個Object hello它有一個namespeak方法。 speak當您通過this魔術變量將其作爲hello.speak調用時,它已經可以訪問hello。所以無論你想做什麼都可以在那裏完成。

您再次創建一個function,其職責是訪問hello.name並提供作爲參數的變量,該變量已可由speak完成,但它只是一個開銷。

對我來說,它有點像:

a = function(){(function(){console.log("Hi")})()} 

a=function(){console.log("Hi")} 

就足夠了。

只要有可能,使代碼簡單,準確,並重點。複雜的代碼並不好,但實際上卻是相反的。我再次不確定你在做什麼,但這是一個普遍的原則。

+0

感謝您的答案,但我有這樣的要求。 – user3666112

+0

然後你可以發佈完整的需求,對於這個簡單的用例,它不是必需的。 – Nishant

+0

由於公司限制,您有時無法發佈實際需求,但這是有些情況。 – user3666112

0

因爲在這種情況下,變量「名」是不變的,我認爲你可以使用一個直接的方法,並做到:

var hello = { 
name: "Vishal", 
speak: function(to){ 
    return function(){ 
     console.log(hello.name+" says hello "+to); 
     }(); 
    } 
} 

或者你可以溝返回功能,並直接用「本」

var hello = { 
name: "Vishal", 
speak: function(to){ 
     console.log(this.name+" says hello "+to); 
    } 
} 

希望它滿足作爲你的答案

+0

您好@shrikanth感謝您的答案,但它是不可能使用某種方式解決使用調用或綁定或應用方法?或者在理解真正的用途方面是否存在嚴重錯誤? – user3666112

+0

呼叫或應用方法被用來調用一個函數,我不知道它在這種情況下可能會有幫助:/ –