node-function-class
Function
subclasses
Easy
Creates a function instance with specified name
and length
and prototype
.
Essentially, it's everything that you need to subclass the ECMAscript Function
built-in type.
You use the function-class/invoke
Symbol to define the logic that will be
executed when the created function instance is invoked.
Installation
Install with npm
:
$ npm install function-class
Examples
One-off:
var createFunction = ;var invoke = ;var fn = ;assert;assert;fninvoke = {return thisname + ' ' + thislength + ' ' + arg;};assert;
Subclass:
var inherits = inherits;var createFunction = ;var invoke = ;{if typeof this !== 'function'return ;thisfoo = foo;assert;assert;};FunctionSubclassprototypeinvoke = {return thisfoo;}// usagevar f = 'foo';assert;assert;assert;
API
The main export is the createFunction()
function, which returns a new Function
instance.
createFunction(name, arity, constructor, arguments) → Function
- name -
String
- thename
to set for the created function, must be a string (even containing spaces, unicode, emoji, and otherwise invalid JS identifiers) - arity -
Number
- thelength
to set for the created function, must be a positive integer - constructor -
Function
- the class constructor to use invoke on the created instance, and inherit from theprototype
of - arguments -
Array
- array of values to invokeconstructor
with
All arguemnts are optional. You must specify the invoke
function to execute on
the created function instance.
var createFunction = ;// inherits from Functionvar fn = ;// inherits from FunctionSubclass with constructors invoked with argsvar args = 'foo' 'bar' ;var fn = ;
invoke → Symbol
You use the invoke
Symbol to define the function to execute when a created
function instance is invoked.
Note that this
in the invoke function is bound to the created function instance
when invoked without a receiver (i.e. "globally"). Otherwise, this
respects the
left-hand receiver, or target when used with .call()
/.apply()
.
var invoke = ;fninvoke = {console;return this;};;// fn has been invoked!assert;assert;