Socket
Socket
Sign inDemoInstall

funstance

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    funstance

make an instance callable like a function


Version published
Maintainers
1
Install size
5.76 kB
Created

Readme

Source

funstance

make an instance callable like a function

build status

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.

Note that obj shouldn't be an Array or possibly other built-in types aside from Object since some of them behave strangely for performance reasons.

install

With npm do:

npm install funstance

license

MIT

Keywords

FAQs

Last updated on 01 Sep 2012

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc