rs.class.js 3.4 KB
/**
 * Class类定义
 */
define(function () {
    return function () {
        var superClass = null;
        var options = {};
        var prototype = {};
        for (var i = 0; i < arguments.length; i++) {
            if (typeof arguments[i] === "function") {
                superClass = arguments[i].prototype;
            } else if (typeof arguments[i] === "object") {
                options = arguments[i];
            }
        }
        if (superClass) {
            prototype = superClass;
            for (var m in options) {
                if (prototype.hasOwnProperty(m)) {
                    if (prototype[m] instanceof Array) {
                        prototype[m].push(options[m]);
                    } else {
                        var array = [prototype[m]];
                        array.push(options[m]);
                        prototype[m] = array;
                    }
                } else {
                    prototype[m] = options[m];
                }
            }
        } else {
            for (var m in options) {
                prototype[m] = options[m];
            }
        }
        var currentClass = function () {
            this.invokeMethod("variable", arguments);
            this.invokeMethod("initialize", arguments);
            this.invokeMethod("handler", arguments);
        };
        currentClass.prototype = prototype;
        currentClass.prototype.invokeMethod = function (name, args) {
            var methods = this.getProperty(name);
            if (methods) {
                if (methods instanceof Array) {
                    for (var i = 0; i < methods.length; i++) {
                        execMethod(this, methods[i]);
                    }
                } else {
                    execMethod(this, methods);
                }
            }
            function execMethod(inst, method) {
                if (typeof method === "function") {
                    method.apply(inst, args);
                } else {
                    this.log(name + "is not function");
                }
            }
        };
        /**
         * 添加属性
         * @param name
         * @param prop
         */
        currentClass.prototype.setProperty = function (name, prop) {
            var tempProp = this.getProperty(name);
            tempProp = prop;
        };
        /**
         * 追加属性
         * @param name
         * @param prop
         */
        currentClass.prototype.appendProperty = function (name, prop) {
            var tempProp = this.getProperty(name);
            if (tempProp) {
                if (methods instanceof Array) {
                    tempProp.push(prop);
                } else {
                    tempProp = prop;
                }
            }
        };
        /**
         * 按名称得到当前属性
         * @param name
         * @returns {*}
         */
        currentClass.prototype.getProperty = function (name) {
            var tempProp;
            for (prop in this) {
                if (prop == name) {
                    tempProp = this[prop];
                    break;
                }
            }
            return tempProp;
        };
        /**
         * 向控制台输出信息
         * @param info
         */
        currentClass.prototype.log = function (info) {
            if (info && console && console.log) {
                console.log(info);
            }
        };
        return currentClass;
    }
});