如何获取绑定到函数的参数?
function add(x){
return x + 1
}
var func = add.bind(null, x)
// how do I get the value of `x` from the `func` variable alone?
如何获取绑定到函数的参数?
function add(x){
return x + 1
}
var func = add.bind(null, x)
// how do I get the value of `x` from the `func` variable alone?
function bind(fn, boundThis, ...args) {
const bound = fn.bind(boundThis, ...args)
bound.__targetFunction__ = fn;
bound.__boundThis__ = boundThis;
bound.__boundArgs__ = args
return bound;
}
您可以像使用Function.prototype.bind
一样使用它,但您还可以访问绑定的值和原始函数:
function addOne(x) {
return x + 1
}
const two = bind(addOne, null, 1)
console.log(two()) // print 2
console.log(two.__boundArgs__) // print [1]
func
变量的上下文中,我没有原始参数。 - Rob[[BoundArgs]]
之下,但我认为这在代码中是不可访问的。 - evolutionxbox[[Scopes]]
,在控制台中,您可以右键单击并按“复制属性路径”,它会给出正确的路径,但尝试评估它会抛出一个错误,说它未定义...只是出于好奇,您如何在控制台中看到[[BoundArgs]]
?我正在尝试使用我的示例,但我什么也看不到。 - Robconsole.dir(func)
。 - KevBot