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) {
    deletethis.prototype[name];
};

functionA() {}
A.add('test', function(){console.log('test');});
var a = newA();
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:

varConstr = (function() {
    functionadd(name, value) {
        this.prototype[name] = value;
    }
    functionremove(name) {
        deletethis.prototype[name];
    }
    returnfunctionmixin(c) {
        c.add = add;
        c.remove = remove;
        return c;
    };
})();

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

I aim to build modulable constructors

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

functionConstBuilder() {
    this.prototype = {};
};

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

var A = newConstBuilder().add('test', function() {
    console.log('test');
}).getConstructor();
var a = newA();
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

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

var r = newRoot();
var c = newChild();

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

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