Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

简单的类封装器 - 让类的定义、继承、扩展更简单些 #1

Open
class4cxy opened this issue Mar 14, 2013 · 0 comments
Open

Comments

@class4cxy
Copy link
Owner

最近真的好闲,也是好事,可以抽空写写blog,分享个简单的 “类” 包装器。宗旨:让类的定义,继承,扩展更简单。

先来看看API够不够明了:

1、定义一个类

CLASS( "parent" );
最简单的一个 parent 类,没有属性,没有方法,没有构造函数;”裸类“

下面定义一个复杂的,有属性、有方法、有构造函数

var parent = CLASS( "parent", function (args) {

    this.name = args.name;
    this.job  = args.job;

}).extend({

    say : function () {

        console.log("Hello, My name is "+this.name)

    }

});

var p = new parent({name : "chen", job : "web"});

p.say() //Hello, My name is chen

这样,parent类中就拥有了自己的属性,方法,构造函数;主要通过 extend 接口去扩展方法,构造函数可有可无;

再来看看继承的API;定义一个 son 类去继承它吧。

var son = CLASS( "son", function (args) {
    // 每个子类都会有一个 _super_ 属性指向父类本身
    this.constructor._super_.call( this, arguments )

}).base( "parent" );

var p = new son({name : "simple", job : "web develop"});

p.say() //Hello, My name is simple

上面定义了一个 son 类,继承了 parent 的方法跟属性,主要通过 base 接口去继承父类的方法

注:不考虑多父类继承,我能想到的原因很简单,javascript 中通过 new 关键字去实例化时,得到的对象中会有个隐性属性如 :proto 指向父类的原型;如果多继承,那这个属性该指向谁?

(属性的继承方式不是很友好,因为我实在没有更好的方法去包装这个接口,可能是实现的思路的一个缺陷。);

当 son 继承了 parent 之后,son 想去扩展自身的方法呢?

son.extend({

    run : function () {

        console.log("Ye, I can run very fast.")

    },
    sing : function () {

        console.log("Ye, I can sing very well.")

    }

})

这样 son 就扩展了两个自己的新方法;

接口差不多就这些了,下面介绍下内部属性:

内部有存储以定义过的 类,不重复定义,可以重写;

每个类都拥有两个静态方法一个:base 和 extend ,主要是继承跟扩展的;

子类都会有一个 super 属性指向父类自身(继承父类属性的时候会用到);

每个类都会拥有一个 index 属性,记录该类在cache中的地址;平时用处不大,主要方便firebug调试。

/**
 * CLASS 类的定义、继承、扩展
 * @param{String} index 类的引用
 * @param[Function] Constructor 类的构造函数
 * @param[boolean] rewrite 重写标志
 * 20130314
 */
function CLASS ( index, Constructor, rewrite ) {

    var self = arguments.callee;

    if ( !self.initialize ) {

        // 存储已定义类
        self.cache = {};
        // 继承类接口
        self.base = function ( index ) {

            var parent = self.cache[index];

            // 只支持单继承机制
            //console.log(parent.toString())
            if ( parent && !this._super_ ) {

                // note : 不使用 new 来继承原型上的方法
                // 因为父类的构造函数中可能传递了参数,却没有做接口检测
                // 每次new都会执行一次构造函数,而导致参数未定义错误
                this._super_ = parent;
                return this.extend( parent.prototype );

            }
            return this;
        };
        // 继承方法接口
        self.extend = function ( prop ) {

            for ( var i in prop ) {

                if ( prop.hasOwnProperty(i) ) this.prototype[ i ] = prop[ i ];

            }
            return this;

        }
        self.initialize = !!1;

    }

    if ( self.cache[index] && !rewrite ) return self.cache[index];

    // 构造函数自身
    Constructor = Constructor || function () {};
    // 每个类都增加一个_index_属性记录该类的引用
    // 对查看父类会有帮助
    Constructor._index_ = index;
    // 继承静态方法
    Constructor.base    = self.base;
    Constructor.extend  = self.extend;

    return self.cache[index] = Constructor;

}

请用Firefox打开 >>DEMO<<

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant