Skip to content

Navigation Menu

Sign in
Sign up

Node.js programming

bellbind edited this page Oct 25, 2013 · 5 revisions

Avoid to crush node runtime by calling a constructor of native object without new

As as module exports, to provide a wrapped JS function to call the constructor with new. e.g.

var nativemod = require("build/Release/mynativemod");
exports.Ctor = function () {
 return new nativemod.Ctor();
};

Generic constructor wrapper by bind with new

If usual functions, you can use apply or call and arguments for forwarding wrapped functions.

For wrapping constructors, combine with ECMAScript5 bind as:

var WrapperCtor = function Ctor() {
 var args = [null].concat([].slice.call(arguments));
 var ctor = RawCtor.bind.apply(RawCtor, args);
 return new ctor;
};

note: wrap constructor with explicit arguments

var WrappedCtor = function Ctor(arg1, arg2) {
 var ctor = RawCtor.bind(null, arg1, arg2);
 return new ctor;
};

Generic version is switched bind call with apply and arguments.

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /