繁体   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