Function.prototype.bind replacement that provides an `unbounded` hidden property on the returned bounded function, that contains the original unbounded function
Function.prototype.bind replacement that provides an unbounded hidden property on the returned bounded function, that contains the original unbounded function
This package functions like Function.prototype.bind, however it exposes a hidden unbounded property on the returned bounded function that contains the original unbounded function.
This is very useful as it means you can do:
(fn.unbounded || fn).length to always get the correct amount of arguments
(fn.unbounded || fn).toString() to always get the source code of any function instead of getting function () { [native code] } for bounded functions
fn.bind(firstContext).unbounded.bind(secondContext) to have fn actually bind to the second context, as fn.bind(firstContext).bind(secondContext) only binds to the first context
To have all bounded functions have the unbounded property, use patch:
import { patch } from 'unbounded'patch()const context = { hello: 'world' }function myFunction() { return this.hello}import { equal } from 'assert'const boundedFunction = myFunction.bind(context)equal(boundedFunction(), context.hello, 'context was correct')equal(boundedFunction.unbounded, myFunction, 'unbounded was correct')
To have only specific functions have the unbounded property, use binder:
import { equal } from 'assert'import { binder } from 'unbounded'const context = { hello: 'world' }function myFunction() { return this.hello}import { equal } from 'assert'const boundedFunction = binder.call(myFunction, context)equal(boundedFunction(), context.hello, 'context was correct')equal(boundedFunction.unbounded, myFunction, 'unbounded was correct')