Prototype for private sub-methods

前端 未结 3 1503

I have code that looks like this:

var baseClass = function() {
// CODE
    var subClass = function() {
        // MORE CODE
    }
}

Adding meth

3条回答
  •  臣服心动
    2021-01-26 03:48

    If you really want to keep the subclass private, you could hide the definitions in a closure:

    var BaseClass = (function() {
        function BaseClass() { ... };
        function SubClass() { ... };
        BaseClass.prototype.foo = function() { ... };
        SubClass.prototype.foo = function() { ... };
        return BaseClass;
    })();
    

    I have personally found this kind of closure-enforced protection to be more trouble than it's worth (ex, makes debugging more difficult)… But if you wanted to do it, that's how you would.

提交回复
热议问题