[原创]类似Java语法的Javascript框架js5

最近闲来无事,写一个类似Java的语法的Javascript框架js5,欢迎大家指正:

 

已出第二版本:http://openxtiger.iteye.com/blog/1707172

 

主代码:

 

 

var Js5 = {version:"5.0.0"};
var __cpackage__;
var __classes__ = {};
var __imports__ = [];

Js5.apply = function(o, c) {
    if (o && c && typeof c == 'object') {
        for (var p in c) {
            o[p] = c[p];
        }
    }
    return o;
};

(function() {

    Js5.apply(Js5, {
        applyIf : function(o, c) {
            if (o) {
                for (var p in c) {
                    if (!Js5.isDefined(o[p])) {
                        o[p] = c[p];
                    }
                }
            }
            return o;
        },
        extend : function() {
            var oc = Object.prototype.constructor;
            return function(sb, sp, overrides) {
                var coverrides = overrides;
                if (!overrides) {
                    coverrides = sp;
                    sp = sb;
                }
                var F = function() {
                },sbp,spp = sp.prototype;
                F.prototype = spp;

                coverrides = coverrides(spp) || {};
                if (!overrides) {
                    sb = coverrides.constructor != oc ? coverrides.constructor : function() {
                        sp.apply(this, arguments);
                    };
                }
                sbp = sb.prototype = new F();
                sbp.constructor = sb;
                //sb.superclass = spp;
                if (spp.constructor == oc) {
                    spp.constructor = sp;
                }

                /*sbp.superclass = (function() {
                 return spp;
                 });*/

                Js5.override(sb, coverrides);  // extend coverrides

                return sb;
            }
        }(),
        override: function(origclass, overrides) {
            if (overrides) {
                var p = origclass.prototype;
                Js5.apply(p, overrides);
            }
        },
        /*override : function(origclass, overrides) {
         if (overrides) {
         var p = origclass.prototype;
         overrides = overrides(p.superclass);

         Js5.apply(p, overrides);

         if (Js5.isIE && overrides.hasOwnProperty('toString')) {
         p.toString = overrides.toString;
         }
         }

         },*/
        isString : function(v) {
            return typeof v === 'string';
        },
        isBoolean : function(v) {
            return typeof v === 'boolean';
        },
        isElement : function(v) {
            return !!v && v.tagName;
        },
        isDefined : function(v) {
            return typeof v !== 'undefined';
        },
        isObject : function(v) {
            return !!v && Object.prototype.toString.call(v) === '[object Object]';
        },
        isDate : function(v) {
            return toString.apply(v) === '[object Date]';
        },
        isPrimitive : function(v) {
            return Js5.isString(v) || Js5.isNumber(v) || Js5.isBoolean(v);
        },
        isNumber : function(v) {
            return typeof v === 'number' && isFinite(v);
        },
        isEmpty : function(v, allowBlank) {
            return v === null || v === undefined || ((Js5.isArray(v) && !v.length)) || (!allowBlank ? v === '' : false);
        },
        isArray : function(v) {
            return toString.apply(v) === '[object Array]';
        },
        isIterable : function(v) {
            //check for array or arguments
            if (Js5.isArray(v) || v.callee) {
                return true;
            }
            //check for node list type
            if (/NodeList|HTMLCollection/.test(toString.call(v))) {
                return true;
            }
            //NodeList has an item and length property
            //IXMLDOMNodeList has nextNode method, needs to be checked first.
            return ((typeof v.nextNode != 'undefined' || v.item) && Js5.isNumber(v.length));
        },
        each : function(array, fn, scope) {
            if (Js5.isEmpty(array, true)) {
                return [];
            }
            if (!Js5.isIterable(array) || Js5.isPrimitive(array)) {
                array = [array];
            }
            for (var i = 0, len = array.length; i < len; i++) {
                if (fn.call(scope || array[i], array[i], i, array) === false) {
                    return i;
                }
            }
            return [];
        },
        namespace : function() {
            var o, d;
            Js5.each(arguments, function(v) {
                d = v.split(".");
                o = window[d[0]] = window[d[0]] || {};
                Js5.each(d.slice(1), function(v2) {
                    o = o[v2] = o[v2] || {};
                });
            });
            return o;
        },
        nameclass:function(c) {
            if (__classes__[c]) return __classes__[c];
            var d = c.split(".");
            var o = window[d[0]] = window[d[0]] || {};
            Js5.each(d.slice(1, d.length - 1), function(v2) {
                o = o[v2] = o[v2] || {};
            });
            return __classes__[c] = o[d[d.length - 1]];
        },
        importclass:function(c) {
            if (__classes__[c]) return __classes__[c];
            var d = c.split(".");
            var o = window[d[0]] = window[d[0]] || {};
            Js5.each(d.slice(1, d.length - 1), function(v2) {
                o = o[v2] = o[v2] || {};
            });
            o = __classes__[c] = o[d[d.length - 1]];
            if (!o) {
                Js5.each(__imports__, function(i) {
                    if ((o = i[d[d.length - 1]])) {
                        return false;
                    }
                });
            }
            return o;
        }
    });

    Js5.ns = Js5.namespace;
})();


/**
 * p1:String ==>package name
 * p2..p-1,Object ==>imports
 * p-1,function ==> package body
 */
function jpackage() {
    var l = arguments.length,i = 0;
    if (l == 0) return;
    var pkf = arguments[l - 1];
    if (Js5.isString(arguments[0])) {
        __cpackage__ = Js5.namespace(arguments[0]);
        i = 1;
    } else {
        __cpackage__ = window;
        i = 0;
    }
    var args = [],vs,ip;
    Js5.each(Array.prototype.slice.call(arguments, i, l - 1), function(v) {
        if (Js5.isString(v)) {
            vs = v.split(";");
            Js5.each(vs, function(v2) {
                if (v2.substr(-2) == ".*") {
                    ip = Js5.namespace(v2.substring(0, v2.length - 2));
                    __imports__.push(ip);
                    args.push(ip);
                } else {
                    args.push(Js5.importclass(v2));
                }
            });
        } else {
            args.push(v);
        }
    });
    pkf.apply(__cpackage__, args);

    __cpackage__ = null;
    __imports__ = [];

}
/**
 * p1:String ==>class name
 * p2,String ==>same package;Object
 * p3,function ==>
 */
function jclass() {
    var l = arguments.length,i = 0;
    if (l < 2) return;
    var clf = arguments[l - 1];

    var c;
    if (Js5.isString(arguments[0])) {
        c = __cpackage__;
    } else {
        c = window;
    }

    var p;
    if (l == 3) {
        var name = arguments[1];
        if (Js5.isString(name)) {
            p = __cpackage__[name];
            if (!p) {
                p = Js5.importclass(name);
            }
        } else {
            p = arguments[1];
        }
    } else {
        p = Object;
    }
    if (!p) {
        throw new Error("[jclass] unrecognized parent class");
    }
    c[arguments[0]] = Js5.extend(p, clf);
}

function jnew() {
    var l = arguments.length,i = 0;
    if (l == 0) return {};

    var args = Array.prototype.slice.call(arguments, 1);

    var name = arguments[0];

    var cls;
    if (Js5.isString(name)) {
        if (__cpackage__ && __cpackage__[name]) {
            cls = __cpackage__[name];
        }

        if (!cls) {
            cls = Js5.importclass(name);
        }
        if (!cls) {
            throw new Error("[jnew] unrecognized  class");
        }
    } else {
        cls = name;
    }


    /*var temp = function() {
     };

     var constructor = cls.prototype.constructor;
     var instanceCls = function() {
     return constructor.apply(this, args);
     };

     temp.prototype = cls.prototype;
     instanceCls.prototype = new temp();
     instanceCls.prototype.constructor = instanceCls;

     return new instanceCls();*/

    var obj = {};
    Js5.override(obj, cls);
    obj.__proto__ = cls.prototype;
    cls.apply(obj, args);
    return obj;


}
例子:

 

 var jui = {};

  jpackage("jui", function() {

    jclass("Component", function() {
      return {
        constructor:function(x) {
          console.log("Component:" + x);
        },
        show:function() {
          console.log("Component");
        }
      }
    });

    jclass("Container", "Component", function(_supr) {
      return {
        constructor:function(x) {
          console.log("Container:" + x);
          _supr.constructor.call(this, x);
        }
      };
    });

  });

  jpackage("jui", function() {

    jclass("Window", "Container", function(_supr) {
      return {
        show:function() {
          console.log("Window");
        }
      }
    });

  });

  jpackage("jui.form", "jui.*", function() {

    jclass("TextField", "Component", function(_supr) {
      return {
        show:function() {
          console.log("TextField");
        }
      }
    });

  });

  jpackage("jui.ex", "jui.Window;jui.*;jui.form.*", function(_W, j) {

    jclass("Window", "jui.Window", function(_supr) {
      return {
        /*constructor:function(x) {
         _supr.constructor.call(this, x);
         },*/
        show:function() {
          console.log("Window Ex");
          _supr.show();
        }
      }
    });


    var x = jnew("Window", "package jui.ex.Window");
    x.show();

    x = jnew(jui.Window, "jui.Window : new in package, use complete class path");
    x.show();

    x = new jui.Window("jui.Window : new in package, use complete class path");
    x.show();

    x = jnew("jui.Window", "jui.Window: new in package, use complete class path");
    x.show();

    x = new j["Window"]("jui.Window: new in package, use import jui.*");
    x.show();

    x = new _W("jui.Window:  new in package, use import jui.Window");
    x.show();

    x = jnew("TextField", "jui.form.TextField  new in package,use import jui.form.*");
    x.show();


  });

  var x = jnew("jui.ex.Window", "global jui.ex.Window");
  x.show();

 

你可能感兴趣的:(JavaScript,框架,js5,类似Java)