简体   繁体   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);
        }();
    }
}

I call this function as - 我将此功能称为-

hello.speak("Vinay");

Actual Output is 实际输出为

says hello to Vinay

Expected output is 预期输出为

Vishal says hello to Vinay

I know hello.name will resolve this but how to resolve it using this so that using call method or apply or bind method this could be resolved. 我知道hello.name将解决此问题,但如何使用this方法解决它,以便可以使用call方法或applybind方法解决此问题。

hello is accessible inside itself. hello可以在内部访问。

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

What is happening in your code: 您的代码中发生了什么:

You have binded a function with an object hello which in turn returns another function that outputs something. 您已将一个函数与一个对象hello绑定在一起,后者又返回另一个输出某些内容的函数。 The outer function speak acts as a closure to the inner function. 外部功能说话是对内部功能的封闭。 Here javascript behaves a little differently because of the way it compiles the code. 在这里javascript的行为因其编译代码的方式而有所不同。 The inner function will not recognise the this variable and rather pick the global this.name . 内部函数将无法识别this变量,而是选择全局this.name

To resolve the issue you will have to do this: 要解决此问题,您将必须执行以下操作:

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");

Understanding use case bind 了解用例绑定

Bind: 绑定:

Creates copy of the function with which bind is called. 创建调用绑定的函数的副本。 You can then pass on the object or the scope which you want to be associated with the this keyword. 然后,您可以传递要与此this关键字关联的对象或范围。

Example: 例:

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();

Now, this is not the real use case of bind, call or apply. 现在,这不是绑定,调用或应用的实际用例。 It was just to show you how your functionality can be achieved using bind. 它只是向您展示如何使用绑定实现功能。

Real use case can be something like this: 实际的用例可以是这样的:

Use Case: 用例:

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

I hope it helps. 希望对您有所帮助。

When you do hello.speak , the this will be become the hello object. 当您执行hello.speakthis将成为hello对象。 Hence this.name is already Vishal . 因此this.name已经是Vishal

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


hello.speak("Vinay");

If you really want to do it the way you asked in the question you can do this: 如果您确实想按照问题中的方式进行操作,则可以执行以下操作:

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

You can also skip passing self because lexical scoping allows you access to that. 您还可以跳过传递self因为lexical作用域允许您访问它。

But picture what you are doing. 但是想象一下你在做什么。 You have an Object hello which has a name and a speak method. 您有一个Object hello ,其中有一个name和一个speak方法。 speak already has access to hello when you invoke it as hello.speak via the this magic variable. 当您通过this magic变量作为hello.speak调用hello时, speak已经可以访问hello So whatever you want to do can be done there. 因此,您想做的任何事情都可以在那完成。

You are again creating a function whose duty is to just access hello.name and the variable supplied as argument which can already be done by speak but it is simply an overhead. 你又创造一个function ,其职责是刚刚接触hello.name并作为参数提供的变量,它可以通过已经做speak ,但它是一个简单的开销。

To me its a bit like: 对我来说有点像:

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

when 什么时候

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

would suffice. 就足够了。

Wherever possible make the code simple, precise and to the point. 尽可能使代码简单,精确并切合实际。 Complicated codes are not better, but its actually the opposite. 复杂的代码不是更好,但实际上相反。 Again I am not sure what you are doing, however this is a general principle. 同样,我不确定您在做什么,但这是一个普遍原则。

Since in this case the variable 'name' is constant i think you can use a direct approach and do: 因为在这种情况下,变量“名称”是常量,所以我认为您可以使用直接方法并执行以下操作:

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

Or you can ditch the return function and do it directly using 'this' 或者,您可以放弃return函数,并直接使用“ this”执行此操作

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

Hope it satisfies as your answer 希望它能满足您的要求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM