Reputation: 35
Like this:
const a = (arg0, arg1, arg2) => {
console.log(arg0);
};
const b = a.bind(null, 1, 2, 3)
Now suppose I only have b, is it possible to get the pre-bind value list of a? I mean the value 1, 2, 3
If it is possible, how?
Upvotes: 2
Views: 789
Reputation: 6282
If you write your own bind function you can attach new properties to it, A function can have properties added to it like any other object.
function bind(fn, thisArg, ...boundArgs) {
const func = function(...args) {
return fn.call(thisArg, ...boundArgs, ...args)
}
// you can hide the properties from public view using
// defineProperties, unfortunately they are still public
Object.defineProperties(func, {
__boundArgs: { value: boundArgs },
__thisArg: { value: thisArg },
__boundFunction: { value: fn }
})
return func
}
const a = (a, b, c) => console.log(a, b, c)
const b = bind(a, null, 1, 2, 3)
b()
console.log(b.__boundArgs)
console.log(b.__thisArgs)
console.log(b.__boundFunction)
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>
The first argument to Function.prototype.bind
is the this
arg.
To answer the question, if you are using chrome you can access the info in the property [[BoundArgs]]
on the bound function. run the code and Check your console
const a = (arg0, arg1, arg2) => {
console.log(arg0, arg1, arg2);
};
const b = a.bind(null, 1, 2, 3)
b()
console.dir(b)
Upvotes: 2