javascript继承与使用super : is this possible?

标签 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?

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

最佳答案

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

这里有几个链接应该有助于理解这一点。

这是基本的模块模式:

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

    return my; 
}(MODULE));

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

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));

这种编码风格需要一些时间来适应,但在这一点上我绝对更喜欢它而不是经典继承。如果此代码没有意义,请查看 Douglas Crockford lectures它应该澄清大部分内容。


处理编辑: 您可以使用 new 创建这些对象的不同实例。运营商。

我建议使用这个扩展对象原型(prototype)的小方法(如果这没有意义,请再次观看 Douglas Crockford 视频)。我忘记了他如此大力推荐的确切原因,但至少它消除了一些混淆,因为 new 运算符与经典语言中的运算符有点不同.不用说,仅使用 new 运算符是不够的。

此函数的作用是使用方法 create 扩展 Object 原型(prototype)。然后...

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

(道格拉斯·克罗克福德本人在原型(prototype)继承链接中有更好的概述)

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

所以使用你的代码...

var a = Object.create(MODULE_TWO),
var b = Object.create(MODULE_TWO);

关于javascript继承与使用super : is this possible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4529519/

相关文章:

javascript - 使用RequireJS : should I define inherited resources

c# - 泛型的继承

javascript - 从 html (webpack) 引用散列的 svg

javascript - 如何在 aurelia 中包含 elasticsearch javascript 库?

javascript - postgres : relation does not exist when the table clearly exists when calling from node. js

javascript - jquery.off() : how to remove a certain click handler only?

没有条件的Javascript递归

具有继承性的 C++ vector

java - 如何管理非 Activity java类中的 fragment ?

继承和knockoutjs