【JavaScript】创建命名空间,Class,LOG

JxUnderscore(function (J, _, root) {


    var isWindow,
        deepObject,
        Namespace,
        Class,
        LOG;


    /**
     * 一个对象是否为window对象
     * @param obj
     * @returns {boolean}
     */
    isWindow = function (obj) {
        return !!obj && obj.window === window;
    };

    /**
     * 用于打印日志的方法
     */
    LOG = (function () {
        if (isWindow(root)) {
            if (!!window.console) {
                return {
                    error: function (s) {
                        window.console.log("[error]" + s);
                    },
                    debug: function () {
                        window.console.log("[debug]" + s);
                    }
                };
            } else {
                return {
                    error: function (s) {
                        window.alert(s)
                    },
                    debug: function () {
                    }
                }
            }
        } else {
            return {
                error: function () {
                },
                debug: function () {
                }
            };
        }
    })();

    /**
     * 创建/获取一个命名空间/包
     * @param namespacePath
     * @param context
     * @returns {*}
     */
    deepObject = function (namespacePath, context) {
        if ("string" !== typeof namespacePath) {
            return undefined;
        }
        var ns = context || root;
        var pathArr = namespacePath.split('.');
        for (var i = 0; i < pathArr.length; i++) {
            var path = pathArr[i];
            ns[path] = ns[path] || {};
            ns = ns[path];
        }
        return ns;
    };


    /**
     * 创建Class类的类
     * Class
     * @class
     * @memberOf Jx
     * @param {Object} option = {extend: superClass} 在option对象的extend属性中指定要继承的对象,可以不写
     * @param {Object} object 扩展的对象
     * @return {Object} Class类
     *
     * @example
     *
     *     var Person = new J.Class({
     *      init : function(name){
     *          this.name = name;
     *          alert("init");
     *      },
     *      showName : function(){
     *          alert(this.name);
     *
     *      }
     *
     *
     *
     *  // 继承Person
     *     var Person2 = new J.Class({extend : Person}, {
     *      init : function(name){
     *          this.name = name;
     *          alert("init");
     *      },
     *      showName : function(){
     *          alert(this.name);
     *
     *      }
     *
     *  });
     *
     * };
     *
     */
    Class = function () {
        var length = arguments.length;
        var option = arguments[length - 1];

        option.init = option.init || function () {
        };

        // 如果参数中有要继承的父类
        if (length === 2) {
            /**
             * @ignore
             */
            var superClass = arguments[0].extend;

            /**
             * @ignore
             */
            var tempClass = function () {
            };
            tempClass.prototype = superClass.prototype;

            /**
             * @ignore
             */
            var subClass = function () {
                this.init.apply(this, arguments);
            };

            // 加一个对父类原型引用的静态属性
            subClass.superClass = superClass.prototype;
            //subClass.superClass = superClass;
            /**
             * @ignore
             */
            subClass.callSuper = function (context, func) {
                var slice = Array.prototype.slice;
                var a = slice.call(arguments, 2);
                var func = subClass.superClass[func];
                //var func = subClass.superClass.prototype[func];
                if (func) {
                    func.apply(context, a.concat(slice.call(arguments)));
                }
            };

            // 指定原型
            subClass.prototype = new tempClass();

            // 重新指定构造函数
            subClass.prototype.constructor = subClass;

            _.extend(subClass.prototype, option);

            /**
             * @ignore
             */
            subClass.prototype.init = function () {
                // 调用父类的构造函数
                // subClass.superClass.init.apply(this, arguments);
                // 调用此类自身的构造函数
                option.init.apply(this, arguments);
            };

            return subClass;

            // 如果参数中没有父类,则单纯构建一个类
        } else if (length === 1) {
            /**
             * @ignore
             */
            var newClass = function () {
                // 加了return,否则init返回的对象不生效
                return this.init.apply(this, arguments);
            };
            newClass.prototype = option;
            return newClass;
        }


    };


    /**
     *  创建/获取一个命名空间/包
     * @param {String} namespacePath
     * @param {function} workspace
     * @constructor
     * @example
     *  J.Namespace("jx.ui.pm",function(pm){
     *     return {
     *         a:"a",
     *         b:"b"
     *      }
     *   });
     *  J.Package("jx.ui.pm",function(pm){
     *      var a=pm.a;
     *      return {
     *         c:"c"+a,
     *         d:"d"
     *      };
     *   });
     */
    Namespace = function (namespacePath, workspace) {
        namespacePath = namespacePath || "";
        workspace = workspace || function () {
        };
        var namespaceObj = deepObject(namespacePath);
        if (namespaceObj) {
            var result = workspace(namespaceObj);
            if (_.isObject(result)) {
                _.extend(namespaceObj, result);
            } else {
                LOG.error("the return value is not an object,namespace :" + namespacePath);
            }
        }
    };



    J.Namespace = Namespace;

    J.Package = Namespace;

    J.Class = Class;

    J.isWindow = isWindow;

    J.createDeepObject = deepObject;

    J.LOG = LOG;


    return {
        Namespace: Namespace,
        Package: Namespace,
        Class: Class,
        isWindow: isWindow,
        createDeepObject: deepObject,
        LOG: LOG
    };

});

  

原文地址:https://www.cnblogs.com/lhp2012/p/4809440.html