javascript面向对象编程基础类库封装jxoop
封装了应用、继承、覆盖、扩展等基础操作。
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() {
var digits = this.toString(16);
if (this < 16) return '0' + digits;
return digits;
},
constrain : function(min, max) {
return Math.min(Math.max(this, min), max);
}
});
Jxoop.override(String, {
gsub: function(pattern, replacement) {
var result = '', source = this, match;
replacement = arguments.callee.prepareReplacement(replacement);
while (source.length > 0) {
if (match = source.match(pattern)) {
result += source.slice(0, match.index);
result += (replacement(match) || '').toString();
source = source.slice(match.index + match[0].length);
} else {
result += source,source = '';
}
}
return result;
},
sub: function(pattern, replacement, count) {
replacement = this.gsub.prepareReplacement(replacement);
count = count === undefined ? 1 : count;
return this.gsub(pattern, function(match) {
if (--count < 0) return match[0];
return replacement(match);
});
},
scan: function(pattern, iterator) {
this.gsub(pattern, iterator);
return this;
},
trunc: function(length, truncation) {
length = length || 30;
truncation = truncation === undefined ? '...' : truncation;
return this.length > length ?
this.slice(0, length - truncation.length) + truncation : this;
},
trim: function() {
return this.replace(/^\s+/, '').replace(/\s+$/, '');
},
stripTags: function() {
return this.replace(/<\/?[^>]+>/gi, '');
},
stripScripts: function() {
return this.replace(new RegExp(Jxoop.ScriptFragment, 'img'), '');
},
extractScripts: function() {
var matchAll = new RegExp(Jxoop.ScriptFragment, 'img');
var matchOne = new RegExp(Jxoop.ScriptFragment, 'im');
return (this.match(matchAll) || []).map(function(scriptTag) {
return (scriptTag.match(matchOne) || ['', ''])[1];
});
},
evalScripts: function() {
return this.extractScripts().map(function(script) {
return eval(script)
});
},
escapeHTML: function() {
var div = document.createElement('div');
var text = document.createTextNode(this);
div.appendChild(text);
return div.innerHTML;
},
unescapeHTML: function() {
var div = document.createElement('div');
div.innerHTML = this.stripTags();
return div.childNodes[0] ? div.childNodes[0].nodeValue : '';
},
toQueryParams: function() {
var pairs = this.match(/^\??(.*)$/)[1].split('&');
return pairs.inject({}, function(params, pairString) {
var pair = pairString.split('=');
params[pair[0]] = pair[1];
return params;
});
},
toArray: function() {
return this.split('');
},
camelize: function() {
var oStringList = this.split('-');
if (oStringList.length == 1) return oStringList[0];
var camelizedString = this.indexOf('-') == 0
? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1)
: oStringList[0];
for (var i = 1, len = oStringList.length; i < len; i++) {
var s = oStringList<i>;
camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
}
return camelizedString;
},
inspect: function() {
return "'" + this.replace(/\\/g, '\\\\').replace(/'/g, '\\\'') + "'";
},
toggle : function(value, other) {
return this == value ? other : value;
}
});
Jxoop.extend(String, {
escape : function(string) {
return string.replace(/('|\\)/g, "\\$1");
},
leftPad : function (val, size, ch) {
var result = new String(val);
if (!ch) {
ch = " ";
}
while (result.length < size) {
result = ch + result;
}
return result.toString();
},
format : function(format) {
var args = Array.prototype.slice.call(arguments, 1);
return format.replace(/\{(\d+)\}/g, function(m, i) {
return args<i>;
});
}
});
Jxoop.override(Array, {
indexOf : function(o) {
for (var i = 0, len = this.length; i < len; i++) {
if (this<i> == o) return i;
}
return -1;
},
remove : function(o) {
var index = this.indexOf(o);
if (index != -1) {
this.splice(index, 1);
}
return this;
},
each: function(iterator) {
for (var i = 0,l = this.length; i < l; i++)
if (iterator(this<i>) === false) break;
},
first: function() {
return this[0];
},
last: function() {
return this[this.length - 1];
}
});
Jxoop.override(Date, {
getElapsed : function(date) {
return Math.abs((date || new Date()).getTime() - this.getTime());
}
});
测试文件a.html
<script type="text/javascript" src="jxoop.js"></script>
<script type="text/javascript">
var A = Jxoop.clazz();
Jxoop.override(A, {
initialize:function(C) {
alert("A" + C);
}
});
var B = Jxoop.clazz();
Jxoop.inherit(B, A, {
initialize:function() {
B.superclass.initialize.apply(this, arguments);
alert("B");
}
});
var C = Jxoop.inherit(B, {
initialize:function() {
alert("C");
C.superclass.initialize.apply(this, arguments);
}
});
new C("arg");
var a = [1,2,3,4];
a.each(function(v) {
alert(v);
if (v == 2) return false;
})
</script>
lunzi
2009-03-04 16:15:07
评论:0
阅读:278
引用:0
