冠军

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

Kendo UI - Class 基类定义

在 kendo 中,使用原型继承机制,Class 是 Kendo 中的基类,定义了函数 extend 用来派生其它类。

复制代码
    function Class() {}

    Class.extend = function(proto) {
        var base = function() {},
            member,
            that = this,
            subclass = proto && proto.init ? proto.init : function () {
                that.apply(this, arguments);
            },
            fn;

        base.prototype = that.prototype;
        fn = subclass.fn = subclass.prototype = new base();

        for (member in proto) {
            if (proto[member] != null && proto[member].constructor === Object) {
                // Merge object members
                fn[member] = extend(true, {}, base.prototype[member], proto[member]);
            } else {
                fn[member] = proto[member];
            }
        }

        fn.constructor = subclass;
        subclass.extend = that.extend;

        return subclass;
    };
复制代码

 首先,我们可以看到

function Class() {}

 

也就是说,Class 是一个函数,在 Kendo 中,所有的类都是通过函数定义,创建对象使用 new 方式来创建。

Class.extend = function(proto) {

 

这里的 extend 是 Class 的静态成员,在 Kendo 中,派生子类都是通过父类的 extend 方式进行处理。方法的参数是子类所扩展的成员对象。

在这个函数内部,首先定义了一个基类函数 base,这显得很奇怪,但是,很快可以发现,这个函数的原型与 Base 的原型其实是同一个,所以通过它们创建出来的对象,行为模式其实是相同的。

var base = function() {},
subclass = proto && proto.init ? proto.init : function () {
    that.apply(this, arguments);
},
base.prototype = that.prototype;
fn = subclass.fn = subclass.prototype = new base();

 

而子类则为 init 函数所定义,在 new 子类对象实例的时候,实际上调用的就是定义子类时提供的 init 函数,在子类的 init 函数内部又调用了父类的构造函数,实现继承机制。

通过将子类的原型设置为父类的对象实例,实现原型继承机制。

子类总是会扩展父类的成员,下面的代码实现为子类的原型对象扩展各种成员。

        for (member in proto) {
            if (proto[member] != null && proto[member].constructor === Object) {
                // Merge object members
                fn[member] = extend(true, {}, base.prototype[member], proto[member]);
            } else {
                fn[member] = proto[member];
            }
        }

 

constructor属性始终指向创建当前对象的构造函数。这里将原型对象的构建函数也设置为子类函数。保证检查类型的时候可以正确工作,可以参看这里的说明:JavaScript:constructor属性

fn.constructor = subclass;

 

使子类也拥有一个同样的 extend 函数,便于继续派生子类。

subclass.extend = that.extend;

 

最后返回子类函数。

 

posted on   冠军  阅读(964)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2011-10-29 Spring.NET 中对于测试支持的示例
点击右上角即可分享
微信分享提示