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

时间:2016-08-30 11:17:50

标签: javascript node.js bind

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])

    }

}

如何在aFunction方法中替换this.selectAction.bind(this)中只有一个数字参数的任何数值?  例如,  " one&#34 ;: this.selectAction.bind(this,number = 0);  " two&#34 ;: this.selectAction.bind(this,number = 1);

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

1 个答案:

答案 0 :(得分:0)

根据bind documentation,您必须按顺序传递参数。

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])
    }

}