How do I get the parameters that have been bound to a function?
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?
You cannot natively, but you can create your own bind-like function with few efforts:
function bind(fn, boundThis, ...args) {
const bound = fn.bind(boundThis, ...args)
bound.__targetFunction__ = fn;
bound.__boundThis__ = boundThis;
bound.__boundArgs__ = args
return bound;
}
You use it like Function.prototype.bind but you additionally get access to the bound values and the original function:
function addOne(x) {
return x + 1
}
const two = bind(addOne, null, 1)
console.log(two()) // print 2
console.log(two.__boundArgs__) // print [1]
funcvariable I do not have the original parameters[[BoundArgs]], but I don't think this is accessible in code.[[Scopes]], in the console you can right click and press "copy property path" and it gives the correct path but trying to evaluate it throws an error saying it's undefined... Just out of curiosity, how are you seeing[[BoundArgs]]in the console? I'm trying to do it with my example but I can't see anything