admin管理员组

文章数量:1390192

It might not be possible, but I'm curious. Is it possible to define a private constructor with a public factory method?

function MyParentClass() {}
MyParentClass.prototype.init = function() { ... }

function MyChildClass() {}
MyChildClass.prototype = new MyParentClass();
MyChildClass.prototype.init = function() {
    ...
    MyParentClass.prototype.init.apply(this);
    ...
}
MyChildClass.Create = function() {
    var instance = new MyChildClass();
    instance.init();
    return instance;
}

Is it possible to hide the 2 constructors and only expose Create()?

Other approaches to this overridable init() approach are wele too. Thank you.

It might not be possible, but I'm curious. Is it possible to define a private constructor with a public factory method?

function MyParentClass() {}
MyParentClass.prototype.init = function() { ... }

function MyChildClass() {}
MyChildClass.prototype = new MyParentClass();
MyChildClass.prototype.init = function() {
    ...
    MyParentClass.prototype.init.apply(this);
    ...
}
MyChildClass.Create = function() {
    var instance = new MyChildClass();
    instance.init();
    return instance;
}

Is it possible to hide the 2 constructors and only expose Create()?

Other approaches to this overridable init() approach are wele too. Thank you.

Share Improve this question asked Apr 26, 2013 at 22:41 Joe FlateauJoe Flateau 1,2352 gold badges20 silver badges34 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 8

I am not sure what you are trying to achieve, but here's an example where MyClass would be a singleton that has a factory method create that allows creating MyClass instances.

//MyClass will be an object with a create method only
var MyClass = (function() {
    function MyClass() {
        this.initialized = false;
    }

    MyClass.prototype = {
        init: function () {
            this.initialized = true;
            return this;
        }
    };

    return {
        create: function () {
            return new MyClass().init();   
        }
    };

})();

var m = MyClass.create();
console.log(m);
console.log(m.constructor); //Will be Object because we replaced the whole prototype

However, I am not sure why you want to have two constructor functions (init and the constructor itself)? Are you trying to abstract the object creation process away because it is plicated?

I suspect that you simply want to move the constructor logic into another function because of the way you are trying to achieve inheritance.

Are you simply trying to avoid calling the constructor logic when you do the following?

MyChildClass.prototype = new MyParentClass();

If it's the case, using Object.create would fix your problem (it is not supported in old browsers, but there's a shim for it -- the shim support the features you would need, but not everything that Object.create does).

function A(test) {
    this.test = test;
}

function B(test) {
    A.call(this, test); //call parent constructor
}
B.prototype = Object.create(A.prototype); //inherit from A

var b = new B('test');

console.log(b);
console.log(b instanceof A); //true

You could also use a pure prototypal approach, without using constructor functions together with the new keyword.

var A = {
        init: function (test) {
            this.test = test;
            return this;
        }
    },
    B = Object.create(A),
    b;

    //override constructor function
    B.init = function (test) {
        return A.init.call(this, test);
    };

b = Object.create(B).init('test');

console.log(b);

本文标签: Private constructor in Javascript with Static membersStack Overflow