I have the following structure:
First file:
Base = function() {
};
Base.something = function() {
return "bla";
};
if (typeof module !== 'undefined' && "exports" in module) {
module.exports = Base;
}
Second file:
var
util = require('util'),
Base = require('./base.js');
FooBar = function() {
};
util.inherits(FooBar, Base);
But FooBar.something is undefined. How can I inherit static methods in node.js?
util.inherits operates on the prototype of a constructor function.
You need to use a simple mixin, commonly referred to as extend. It basically just copies all the properties of one (or many) objects into a “destination” object. (in this case, your constructor function)
If you’re using underscore.js, you can use
_.extend(FooBar, Base);