简体   繁体   English

将函数分配给数组的元素。 如何绑定函数的输出?

[英]assigning function to elements of array . How to bind output of function?

I am trying to create a 'secret' guestList to practice some closures and binding in JS. 我正在尝试创建一个“秘密” guestList,以在JS中练习一些关闭和绑定。 I am currently stuck because I need to use binding so the value of i updates after every iteration but I am really new to biding and I am having trouble wrapping my head around this... how do I call my variable code from the closure? 我目前处于困境,因为我需要使用绑定,所以每次迭代后i的值都会更新,但是我对出价真的很陌生,我对此一无所知。如何从闭包中调用变量code how do I correctly bind the guesName to my checkCode function? 如何将guesName正确绑定到我的checkCode函数? :/ :/

Here my code : 这是我的代码:

function guestListFns(guestList, secretCode){
    var topSecretList = []; 
    function codeChecker(code) {
        if (code === secretCode) {
            return guestName;
        } else {
            return "Secret-Code: Invalid";
        }
    };
    for(var i = 0 ; i < guestList.length; i += 1){
        var guestName = guestList[i]; 
            topSecretList.push(codeChecker.call(this.guestName, code));
    }
    console.log(topSecretList);

        return topSecretList; 
}

my testing values : 我的测试值:

    var guestListFns = guestListFns(["Gabriel", "Ben", "Dan", "Griffin", "Cang", "Kate", "Chris"], 512);

var guest = guestListFns[1](512);
console.log(guest);

my return value so far: 到目前为止我的返回值:

"code is not defined" 

Also, I have already figured out how to implement this function simply using map. 另外,我已经弄清楚了如何仅使用map来实现此功能。 But what I meant with this exercise is to practice binding so I can understand the concept. 但是我对本练习的意思是练习约束力,这样我才能理解这个概念。

Thanks! 谢谢!

You dont wanna call , but you want to bind : 您不想call ,但您想bind

codeChecker.bind({ guestName })

So now inside codeChecker you can access 因此,现在在codeChecker中,您可以访问

this.guestName

Actually you are overcomplicating things: 实际上,您使事情变得过于复杂:

 const guestListFns = (arr, secret) =>
   arr.map(el => code => code === secret ? el : "nope");

If I understand what you are trying, I think call() is the wrong way to go about this. 如果我了解您在尝试什么,我认为call()是执行此操作的错误方法。 You want an array of partial functions where each function already has the name. 您需要一个局部函数数组,其中每个函数都已经具有名称。 You can use bind() for this. 您可以为此使用bind() call() actually invokes the function, which isn't what you really want here. call()实际上会调用该函数,而这并不是您真正想要的。 bind() returns a new function and allows you to set this and/or some of the arguments: bind()返回一个新函数,并允许您设置this和/或某些参数:

 function guestListFns(guestList, secretCode){ var topSecretList = []; function codeChecker(guestName, code) { if (code === secretCode) { return guestName; } else { return "Secret-Code: Invalid"; } }; for(var i = 0 ; i < guestList.length; i += 1){ var guestName = guestList[i]; topSecretList.push(codeChecker.bind(null, guestName)); } return topSecretList; } var guestListFns = guestListFns(["Gabriel", "Ben", "Dan", "Griffin", "Cang", "Kate", "Chris"], 512); console.log(guestListFns[1](512)); // ben console.log(guestListFns[2](512)); // dan console.log(guestListFns[1](112)); // bad code 

Might this be what you're looking for? 可能这就是您要找的东西吗?

 function guestListFns(guestList, secretCode){ var topSecretList = []; function codeChecker(code) { if (code === secretCode) { return this.guestName; } else { return "Secret-Code: Invalid"; } }; for(var i = 0 ; i < guestList.length; i += 1){ var guestName = guestList[i]; topSecretList.push(codeChecker.bind({guestName:guestName})); } console.log(topSecretList); return topSecretList; } var gfn = guestListFns(["Gabriel", "Ben", "Dan", "Griffin", "Cang", "Kate", "Chris"], 512); gfn.forEach(f => console.log(f(512))); 

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

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