栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

简单的JS多重继承示例

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

简单的JS多重继承示例

复制代码 代码如下:
$defined = function (v) {
    return v != undefined;
}

Class = function () {
    var base = {};
    for (var k=0; k    //{{new arguments[k]() with custom constructor field.
        var o = arguments[k].prototype;
        o.constructor = arguments[k];
        arguments[k].call(o);
    //}}
        for (key in o) base[key] = o[key];
    }
    function Klass () {
        // for every class one object cache.
        var clso = null;
        function klass() {
            if (arguments.length<=0 && clso!=null) {
                // hit cache.
                return clso;
            }
            if ($defined(this.constructor.init)) {
                // use init() for class initialization.
                this.constructor.init.apply(this, arguments);
            }
            clso = this;
        }
        klass.prototype = base;
        return klass;
    }
    return Klass();
}

A = new Class();
A.init = function () {
    this.x = 400;
    this.y = 300;
}
B = new Class(A);
B.init = function () {
    this.y = 200;
    this.z = 100;
}
C = new Class(B);
C.init = function () {
    this.z = 0;
}
c = new C();
alert(c.x);
alert(c.y);
alert(c.z); 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/119441.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号