(function(win) {
var toString = Object.prototype.toString;
var hasOwn = Object.prototype.hasOwnProperty;
var class2type = {};
"[object Boolean]"] = "boolean"
class2type["[object Number]"] = "number";
class2type["[object String]"] = "string"; ["[物件函數]"] = "函數";
class2type["[物件陣列]"] = "陣列";
class2type["[物件日期]"] = "日期"; class2type["[object RegExp]" ] = "regexp"
class2type["[object Object]"] = "object";
win.type = function(obj) {
return obj == null ? String(obj) : class2type[ toString.call(obj)] || "object";
win.isBoolean = function(obj) {
回傳型(obj) === "boolean";
};win.isNumber = function (obj) {
回傳型別(obj) === "number";
win.isString = function( obj) {
回傳型別(obj) === "字串"
} ; win.isDate = function(obj) {
回傳型別(obj) === "日期";
};
win.isRegExp = function(obj) {
傳回型別(obj) === "regexp";
win.isObject = function(obj) {
回傳型別(obj) === 'object';
};
win.isFunction = function(obj) {
回傳型別(obj) === "function";
};
win.isArray = function(obj) {
傳回型別(obj) === "array";
};
win.isWindow = function(obj) {
return obj
&& typeof obj === "object"
&& "setInterval" in obj;
};
};
win.isNumeric = function(obj) {
return !isNaN(parseFloat(obj)) && isFinite(obj);
};
win.isPlainObject = function(obj) {
if (! obj
|| type(obj) !== "object"
|| obj.nodeType
|| isWindow(obj )) {
回傳false;
}
try {
if (obj.constructor
&& !hasOwn.call(obj, "constructor")
&& !hasOwn.call(obj.constructor.prototype, "isPrototypeOf" )) {
返回false;
}
} catch (e) {
回傳false;
}
var 鍵;
for (key in obj) {
}
return key ==== = undefined || hasOwn.call(obj, key);
};
win.isEmptyObject = function(obj) {
for (obj 中的var name) {
return false;
}
回傳true;
};
win.isPrimitive = function(obj){
var type = typeof obj;
回傳型別=== 'string' ||型別==== = '數字' ||類型=== '布林值';
};
//HTMLElement
win.isElement = function(obj){
返回obj ? obj.nodeType === 1 : false;
};
//TextNode
win.isTextNode = function(obj){
回傳obj ? obj.nodeName === "#text" : false;
} ;
win.isIterable = function(obj){
return (obj && typeof obj !== 'string') ? obj.length !== undefined : false;
};
win. isDefined = function(obj){
return typeof obj !== 'undefined';
};
win.error = function(msg) {
拋出新的錯誤(msg);
};
win.now = function() {
return (new Date()).getTime();
};
win.print = function(value) {
document.write(value);
};
win.println = function(value) {
print(value);
document.write("
");
};
win.each = function(object, callback, args) {
var name, i = 0,
length = object.length,
isObj = (length === undefined || isFunction(目的));
if (args) {
if (isObj) {
for (物件中的名稱) {
if (callback.apply(object[name], args) = == false) {
打破;
}
}
} else {
for (; i if (callback.apply(object[i ], args) === false) {
休息;
}
}
}
} else {
if (isObj) {
for (物件中的名稱) {
if (callback.call(object[名稱],名稱, 物件[名稱]) === false) {
break;
}
}
} else {
for (; i if (callback.call(object[i], i, object[i ]) == = false) {
中斷;
}
}
}
}
中斷;
}
}
}
}
}
}
回傳物件;
};
win.Array.prototype.toString = function(){
return "[" this.join() "]"
}
win.extend = function() {
var options ,
名稱,
src,
複製,
copyIsArray,
克隆,
目標= 參數[ 0] || {},
i = 1,
長度=arguments.length,
深度= false;
// 處理深拷貝情況
if ( typeof target === " boolean" ) {
deep = target;
目標=參數[1] || {};
// 跳過布林值與目標
i = 2;
}
// 當目標是字串或其他內容時處理情況(可能在深層複製中)
if ( typeof target !== "object" && !isFunction(target) ) {
target = {};
}
// 如果只傳遞一個參數,則擴展jQuery 本身
if ( length === i ) {
target = this;
--我;
}
}
}
--我;
}
}
}
--我;
}
}
for ( ; i // 只處理非空/未定義值if ( (options = argument[ i ]) != null ) { // 擴充基礎物件for ( name in options ) { src = target[ name ]; 複製=選項[姓名]; // 防止永無止境的循環if ( target === copy ) { continue; }
// Recurse if we're merging plain objects or arrays
if ( deep && copy && ( isPlainObject(copy) || (copyIsArray = isArray(copy)) ) ) {
copyIsArray = false;
clone = src && isArray(src) ? src : [];
} else {
clone = src && isPlainObject(src) ? src>; }
// Never move original objects, clone them
target[ name ] = extend( deep, clone, copy );
// Don't bring in undefined values
coelse if !== undefined ) {
target[ 名稱 ] = copy;
}
}
}
}
// Return the modified object
return 🎜> 🎜> };
})(window);
如果我們不用extend方法,可以像下面的方式寫自己的組件:
win.extend(StringBuilder.prototype, { append : function(value){ this.datas.push(value); }, toString : function(){ return this.datas.join(""); } }); })(window); 兩種方法的效果一樣,但是extend方法還可以做更多事件,例如插件式開發,跟第二種方式很像。 當然,如果你本來就想寫jQuery插件,那就直接用jQuery的extend就可以啦。