Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 3be8cc04c2
Fetching contributors…

Cannot retrieve contributors at this time

file 75 lines (52 sloc) 1.154 kb

funstance

make an instance callable like a function

example

funstantiate a prototypical object

foo.js:

var inherits = require('inherits');
var Stream = require('stream');

module.exports = Foo;
inherits(Foo, Stream);

function Foo (x) {
    this.x = x;
}

Foo.prototype.beep = function () {
    this.emit('beep', 'boop');
};

main.js:

var funstance = require('funstance');
var Foo = require('./foo');

var obj = new Foo(4);
var fobj = funstance(obj, function (n) {
    return n * obj.x
})

console.log(fobj(111));
fobj.on('beep', console.log);
fobj.beep();

Note that .on() is defined all the way in EventEmitter, which is 3 times removed up the prototype chain from fobj, yet fobj.on() still works despite being a function.

$ node main.js
444
boop

methods

var funstance = require('funstance')

var fobj = funstance(obj, fn)

Return a function with all the properties and prototypical methods as obj. When fobj() is called, fn() will fire with the arguments and this set to the obj.

install

With npm do:

npm install funstance

license

MIT

Something went wrong with that request. Please try again.