返回首页
当前位置: 主页 > 网络编程 > Jsp实例教程 >

javascript面向对象编程基础类库封装jxoop

时间:2010-06-03 21:06来源:知行网www.zhixing123.cn 编辑:麦田守望者

代码如下:

 

封装了应用、继承、覆盖、扩展等基础操作。
jxoop.js
var Jxoop = {
version: '1.0.0'
}
// for old browsers
window["undefined"] = window["undefined"];

//extend and override
Jxoop.apply = function(o, c, defaults) {
if (defaults) {
Jxoop.apply(o, defaults);
}
if (o && c && typeof c == 'object') {
for (var p in c) {
o<p> = c<p>;
}
}
return o;
};

(function() {
var idSeed = 0;
var ua = navigator.userAgent.toLowerCase();
var isStrict = document.compatMode == "CSS1Compat",
isOpera = ua.indexOf("opera") > -1,
isChrome = ua.indexOf("chrome") > -1,
isSafari = !isChrome && (/webkit|khtml/).test(ua),
isSafari3 = isSafari && ua.indexOf('webkit/5') != -1,
isIE = !isOpera && ua.indexOf("msie") > -1,
isIE7 = !isOpera && ua.indexOf("msie 7") > -1,
isIE8 = !isOpera && ua.indexOf("msie 8") > -1,
isGecko = !isSafari && !isChrome && ua.indexOf("gecko") > -1,
isGecko3 = isGecko && ua.indexOf("rv:1.9") > -1,
isBorderBox = isIE && !isStrict,
isWindows = (ua.indexOf("windows") != -1 || ua.indexOf("win32") != -1),
isMac = (ua.indexOf("macintosh") != -1 || ua.indexOf("mac os x") != -1),
isAir = (ua.indexOf("adobeair") != -1),
isLinux = (ua.indexOf("linux") != -1),
isSecure = window.location.href.toLowerCase().indexOf("https") === 0;
if (isIE && !isIE7) {
try {
document.execCommand("BackgroundImageCache", false, true);
} catch(e) {
}
}
Jxoop.apply(Jxoop, {
scriptFragment: '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)',
emptyFn : function() {
},
defaultFn:function(p) {
return p;
},
clazz: function() {
return function() {
(this.initialize || Jxoop.emptyFn).apply(this, arguments);
}
},
extend : function(o, c) {
if (o && c) {
for (var p in c) {
if (typeof o<p> == "undefined") {
o<p> = c<p>;
}
}
}
return o;
},
inherit : function() {
var io = function(o) {
for (var m in o) {
this[m] = o[m];
}
};
var oc = Object.prototype.constructor;
return function(sb, sp, overrides) {
if (typeof sp == 'object') {
overrides = sp;
sp = sb;
sb = overrides.constructor != oc ? overrides.constructor : function() {
sp.apply(this, arguments);
};
}
var F = function() {
}, sbp, spp = sp.prototype;
F.prototype = spp;
sbp = sb.prototype = new F();

sbp.constructor = sb;
sb.superclass = spp;

if (spp.constructor == oc) {
spp.constructor = sp;
}
sb.override = function(o) {
Jxoop.override(sb, o);
};
sbp.override = io;
Jxoop.override(sb, overrides);
sb.inherit = function(o) {
Jxoop.inherit(sb, o);
};
return sb;
};
}(),
override : function(origclass, overrides) {
if (overrides) {
var p = origclass.prototype;
for (var method in overrides) {
p[method] = overrides[method];
}
if (Jxoop.isIE && overrides.toString != origclass.toString) {
p.toString = overrides.toString;
}
}
},
namespace : function() {
var a = arguments, o = null, i, j, d, rt;
for (i = 0; i < a.length; ++i) {
d = a<i>.split(".");
rt = d[0];
eval('if (typeof ' + rt + ' == "undefined"){' + rt + ' = {};} o = ' + rt + ';');
for (j = 1; j < d.length; ++j) {
o[d[j]] = o[d[j]] || {};
o = o[d[j]];
}
}
},
typeOf : function(o) {
if (o === undefined || o === null) {
return false;
}
if (o.htmlElement) {
return 'element';
}
var t = typeof o;
if (t == 'object' && o.nodeName) {
switch (o.nodeType) {
case 1: return 'element';
case 3: return (/\S/).test(o.nodeValue) ? 'textnode' : 'whitespace';
}
}
if (t == 'object' || t == 'function') {
switch (o.constructor) {
case Array: return 'array';
case RegExp: return 'regexp';
case Date: return 'date';
}
if (typeof o.length == 'number' && typeof o.item == 'function') {
return 'nodelist';
}
}
return t;
},
isEmpty : function(v, allowBlank) {
return v === null || v === undefined || (!allowBlank ? v === '' : false);
},
nvl : function(v, defaultValue, allowBlank) {
return Jxoop.isEmpty(v, allowBlank) ? defaultValue : v;
},
isArray : function(v) {
return v && typeof v.length == 'number' && typeof v.splice == 'function';
},

isDate : function(v) {
return v && typeof v.getFullYear == 'function';
},

isStrict : isStrict,
isSecure : isSecure,
isReady : false,
enableGarbageCollector : true,
enableListenerCollection:false,
SSL_SECURE_URL : "javascript:false",
isOpera : isOpera,
isChrome : isChrome,
isSafari : isSafari,
isSafari3 : isSafari3,
isSafari2 : isSafari && !isSafari3,
isIE : isIE,
isIE6 : isIE && !isIE7 && !isIE8,
isIE7 : isIE7,
isIE8 : isIE8,
isGecko : isGecko,
isGecko2 : isGecko && !isGecko3,
isGecko3 : isGecko3,
isBorderBox : isBorderBox,
isLinux : isLinux,
isWindows : isWindows,
isMac : isMac,
isAir : isAir,
useShims : ((isIE && !isIE7) || (isMac && isGecko && !isGecko3))
});

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

Jxoop.override(Function, {
bind : function(obj, args, appendArgs) {
var method = this;
return function() {
var callArgs = args || arguments;
if (appendArgs === true) {
callArgs = Array.prototype.slice.call(arguments, 0);
callArgs = callArgs.concat(args);
} else if (typeof appendArgs == "number") {
callArgs = Array.prototype.slice.call(arguments, 0);
var applyArgs = [appendArgs, 0].concat(args);
Array.prototype.splice.apply(callArgs, applyArgs);
}
return method.apply(obj || window, callArgs);
};
},
defer : function(millis, obj, args, appendArgs) {
var fn = this.createDelegate(obj, args, appendArgs);
if (millis) {
return setTimeout(fn, millis);
}
fn();
return 0;
},
createCallback : function() {
var args = arguments;
var method = this;
return function() {
return method.apply(window, args);
};
},
createSequence : function(fcn, scope) {
if (typeof fcn != "function") {
return this;
}
var method = this;
return function() {
var retval = method.apply(this || window, arguments);
fcn.apply(scope || this || window, arguments);
return retval;
};
},
createInterceptor : function(fcn, scope) {
if (typeof fcn != "function") {
return this;
}
var method = this;
return function() {
fcn.target = this;
fcn.method = method;
if (fcn.apply(scope || this || window, arguments) === false) {
return;
}
return method.apply(this || window, arguments);
};
}
});

Jxoop.override(Number, {
toColorPart: function() {

------分隔线----------------------------
标签(Tag):Javascript
------分隔线----------------------------
推荐内容
猜你感兴趣