我如何在this.function.bind(this)中替换任何数值作为参数

How can I substitute any numeric value for argument in this.function.bind(this)

本文关键字:this 替换 任何数 参数 function bind      更新时间:2023-09-26
class hoge{
    constructor(key) {
        this.Idx = require("./myIndex.json");
    }
    foo(id, concepts){
        var aFunction = {
            "one": this.selectAction.bind(this),
            "two": this.selectAction.bind(this)
    }
    selectAction(id, concepts, number){
        return (id + concepts + this.Idx[number])
    }
}

我如何在this. selectaction .bind(this)中的函数方法中替换任何数值仅为number的一个参数?例如,"一":this.selectAction。Bind (this, number = 0);"两个":this.selectAction。Bind (this, number = 1);

如果我这样写,返回这个错误 ReferenceError: number is not defined

根据bind文档,你必须按顺序传递参数。

class hoge{
    constructor(key) {
        this.Idx = require("./myIndex.json");
    }
    foo(id, concepts){
        var aFunction = {
            one: this.selectAction.bind(this, id, concepts, 1),
            two: this.selectAction.bind(this, id, concepts, 2)
    }
    selectAction(id, concepts, number){
        return (id + concepts + this.Idx[number])
    }
}