Javascript inherance和super的使用:这可能吗?

Tot*_*.js 4 javascript inheritance

if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {
        }

        F.prototype = o;
        var f = new F();

        if(f.init){
            f.init();
        };

        return f;
    };
}

var inherit = function(P, C) {
    var i;
    for(i in P) {
        // if is the current parent
        if(P.hasOwnProperty(i) === false) {
            continue;
        };

        // define the uper property
        C.uper = {};

        // methods
        if(typeof P[i] === 'function') {
            // set as super
            C.uper[i] = P[i];
            // if child already defined, skip
            if(typeof C[i] === 'function') {
                continue;
            };
            C[i] = P[i];
        }

        // properties 
        else {
            // if child already defined a property, skip
            if(!(typeof C[i] === 'undefined')) {
                continue;
            };
            C[i] = P[i];
        }
    }
    return C;
}


var Parent1 = (function(){
    var that = {};

    // private var
    var _name = 'Parent1';

    // public var
    that.lastName = 'LastName';

    // public method
    that.getName = function(){
        // if this.uper.getName.call(this)
        return _name + this.lastName;
        // else
        // return _name + that.lastName;
    }

    // return the literal object
    return that;
}());

var Parent2 = {
    // fake private var
    _name: 'Parent2',

    // public method
    getName: function(){
        // as we call this method with the call method
        // we can use this
        return this._name;
    }
}

var Child1 = inherit(Parent1, (function(){
    var that = {};

    // overriden public method
    that.getName = function(){
        // how to call the this.uper.getName() like this?
        return 'Child 1\'s name: ' + this.uper.getName.call(this);
    }

    that.init = function(){
        console.log('init');
    }

    // return the literal object
    return that;
}()));

var Child2 = inherit(Parent2, {
    getName: function(){
        // how to call the this.uper.getName() like this?
        return 'Child 2\'s name: ' + this.uper.getName.call(this);
    }
});

var child1 = Object.create(Child1);
// output: Child 1's name: Parent1LastName
console.log(child1.getName());

var child2 = Object.create(Child2);
// output: Child 2's name: Parent2
console.log(child2.getName());
// how to call the this.uper.getName() like this?
Run Code Online (Sandbox Code Playgroud)

如何像这样调用this.uper.getName()?

Der*_*air 7

Javascript使用Prototypal继承.所以基本上对象继承对象(一切都是对象)

以下是一些应该有助于理解这一点的链接.

这是基本的模块模式:

var MODULE = (function (my) { 
    my.anotherMethod = function () { 
        // added method... 
    }; 

    return my; 
}(MODULE));
Run Code Online (Sandbox Code Playgroud)

然后你可以做这样的事情来模仿继承:

var MODULE_TWO = (function (old) { 
    var my = {}, 
        key; 

    for (key in old) { 
        if (old.hasOwnProperty(key)) { 
            my[key] = old[key]; 
        } 
    } 

    var super_moduleMethod = old.moduleMethod; 
    my.moduleMethod = function () { 
        // override method on the clone, access to super through super_moduleMethod 
    }; 

    return my; 
}(MODULE));
Run Code Online (Sandbox Code Playgroud)

这种编码风格需要一些习惯,但我绝对更喜欢它在这一点上的经典继承.如果这段代码没有意义,请查看道格拉斯克罗克福德讲座,它应该澄清大部分内容.


寻址编辑: 您可以使用new运算符创建这些对象的不同实例.

要么

我建议使用这个扩展Object Prototype的小方法(如果没有意义,请参阅Douglas Crockford视频).我忘记了他为什么如此强烈推荐的确切原因,但至少它消除了一些混淆,因为new运算符与经典语言略有不同.不用说仅使用new操作员是不够的.

这个函数的作用是使用方法create扩展Object原型.那么......

  1. 在包含的命名空间中定义函数F.
  2. 将函数F的原型分配给传递的对象
  3. 返回新构造的Object.

(douglas crockford本人在原型继承链接中更好地概述)

if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}
newObject = Object.create(oldObject);
Run Code Online (Sandbox Code Playgroud)

所以使用你的代码......

var a = Object.create(MODULE_TWO),
var b = Object.create(MODULE_TWO);
Run Code Online (Sandbox Code Playgroud)