Skip to content Skip to sidebar Skip to footer

JavaScript Build A Constructor Of Constructors

Here is a simple example of what I want : var ConstBuilder = function() { var constructor = function() {} ; constructor.prototype = {} ; return constructor ; } ; Const

Solution 1:

Please make sure you have understood the difference between the .prototype property and the internal inheritance-prototype.


The code will fail as A is not an instance of ConstBuilder. Is there a way to have a function as an instance of another?

A is, as every constructor needs to be, a Function. So if you just define your add and remove methods on the Function.prototype, it will work:

Function.prototype.add = function(name, value) {
    this.prototype[name] = value;
};
Function.prototype.remove = function(name) {
    delete this.prototype[name];
};

function A() {}
A.add('test', function(){console.log('test');});
var a = new A();
a.test(); // test

A.remove('test');
a.test; // undefined

There is no possibility however to let a function inherit from something else than Function.prototype - see Can a JavaScript object have a prototype chain, but also be a function?. If you don't want to modify the native Function.prototype object, you still can use the mixin pattern:

var Constr = (function() {
    function add(name, value) {
        this.prototype[name] = value;
    }
    function remove(name) {
        delete this.prototype[name];
    }
    return function mixin(c) {
        c.add = add;
        c.remove = remove;
        return c;
    };
})();

var A = Constr(function() {…});
A.add("test", …);
var a = new A();
a.test(); // test

I aim to build modulable constructors

You could use the builder pattern, as you just have seem to tried.

function ConstBuilder() {
    this.prototype = {};
};

ConstBuilder.prototype = {
    add: function(name, value) {
        this.prototype[name] = value;
    },
    remove: function(name) {
        delete this.prototype[name];
    },
    getConstructor: function() {
        var constructor = function() {};
        constructor.prototype = this.prototype;
        this.prototype.constructor = constructor;
        return constructor;
    }
};

var A = new ConstBuilder().add('test', function() {
    console.log('test');
}).getConstructor();
var a = new A();
a.test(); // test

To remove functions later, you would need to save a reference to the builder.


Solution 2:

I think that you are looking for an example of how to do JavaScript's "prototypical inheritance". When JavaScript looks for a property on an object, it first checks the object itself. Next it checks the prototype. However, since everything in JavaScript is an object and the prototype is an object

function Root(){}
Root.prototype.fromRoot = function() { console.log("I'm on Root's prototype."); };
function Child(){}
Child.prototype = new Root();
Child.prototype.fromChild = function() { console.log("I'm on Child's prototype."); };

var r = new Root();
var c = new Child();

r.fromRoot();  // works
c.fromRoot();  // works
c.fromChild(); // works
r.fromChild(); // fails

Post a Comment for "JavaScript Build A Constructor Of Constructors"