簡體   English   中英

如何在此代碼中訪問名稱

[英]How to make name accessible in this code

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方法解決此問題。

hello可以在內部訪問。

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

您的代碼中發生了什么:

您已將一個函數與一個對象hello綁定在一起,后者又返回另一個輸出某些內容的函數。 外部功能說話是對內部功能的封閉。 在這里javascript的行為因其編譯代碼的方式而有所不同。 內部函數將無法識別this變量,而是選擇全局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");

了解用例綁定

綁定:

創建調用綁定的函數的副本。 然后,您可以傳遞要與此this關鍵字關聯的對象或范圍。

例:

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

希望對您有所幫助。

當您執行hello.speakthis將成為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 ,其中有一個name和一個speak方法。 當您通過this magic變量作為hello.speak調用hello時, speak已經可以訪問hello 因此,您想做的任何事情都可以在那完成。

你又創造一個function ,其職責是剛剛接觸hello.name並作為參數提供的變量,它可以通過已經做speak ,但它是一個簡單的開銷。

對我來說有點像:

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

什么時候

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

就足夠了。

盡可能使代碼簡單,精確並切合實際。 復雜的代碼不是更好,但實際上相反。 同樣,我不確定您在做什么,但這是一個普遍原則。

因為在這種情況下,變量“名稱”是常量,所以我認為您可以使用直接方法並執行以下操作:

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

或者,您可以放棄return函數,並直接使用“ this”執行此操作

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

希望它能滿足您的要求

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM