如何用 this.function.bind(this) 中的参数替换任何数值
How can I substitute any numeric value for argument in this.function.bind(this)
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": this.selectAction.bind(这个, 数字 = 0);
"two": this.selectAction.bind(这个, number = 1);
如果我这样写,return这个错误
ReferenceError: number is not defined
根据 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])
}
}
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": this.selectAction.bind(这个, 数字 = 0); "two": this.selectAction.bind(这个, number = 1);
如果我这样写,return这个错误
ReferenceError: number is not defined
根据 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])
}
}