jquery class inheritance

后端 未结 6 2065
清歌不尽
清歌不尽 2020-12-24 02:41
var A=function(){
};

$.extend(A.prototype, {
    init:function(){
        alert(\'A init\');
    }
});
var B=function(){

};

$.extend(B.prototype,A.prototype,{
            


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-24 03:04

    I use the same pattern and I like its conciseness.

    About the lack of the "super" keyword, that's not really a problem. Thanks to Function.prototype.call() operator, you can call any function within the context of any object. So the sequence to call A.prototype.init() from B.prototype.init() is:

    A.prototype.init.call(this, some parameters ...);

    Also, don't forget that you may call A constructor from B constructor like this:

    B = function(key, name) {
        A.call(this, key);
        this.name = name;
    };
    

    An experimented JS coder will know what happens.

    So to conclude: not perfect but close enough.

提交回复
热议问题