个人封装库,第一个版本(超烂)

来源:互联网 发布:北京楼盘数据 编辑:程序博客网 时间:2024/06/06 17:08
function myNeedExtend() {}myNeedExtend.prototype = {    constructor:myNeedExtend,    //判断是什么媒体设备的浏览器    browserRedirect: function () {        var sUserAgent = navigator.userAgent.toLowerCase();        var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";        var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";        var bIsMidp = sUserAgent.match(/midp/i) == "midp";        var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";        var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";        var bIsAndroid = sUserAgent.match(/android/i) == "android";        var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";        var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";        //调用方法,首先判断是移动端还是PC端,然后根据浏览器的分辨率判断是pad还是phone还是minphone        if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {            if (bIsIpad) {                return "pad";            } else if (document.body.clientWidth > 767 && document.body.clientHeight > 767) {                return "pad";            } else if (document.body.clientWidth < 400 || document.body.clientHeight < 400) {                return "minphone";            } else {                return "phone";            }        } else {            return "pc";        }    },    //深拷贝和浅拷贝(设置,对象,深/浅)    cloneObj: function (settings, obj, boolean) {        var bool = boolean || false;        if (typeof obj != "object") {            return;        }        //首先判断是深拷贝还是浅拷贝 true 深   false 浅        if (bool) {            for (var i in obj) {                if (typeof obj[i] == "object") {                    if (obj[i].constructor.prototype == settings[i].constructor.prototype) {                        this.cloneObj(settings[i], obj[i], true);                    } else {                        if (obj[i] instanceof Array) {                            settings[i] = [];                        } else {                            settings[i] = {};                        }                        this.cloneObj(settings[i], obj[i], true);                    }                } else {                    settings[i] = obj[i];                }            }        } else {            for (var i in obj) {                settings[i] = obj[i];            }        }    },// 是否加载完成isReady: true,error: function( msg ) {throw new Error( msg );},noop: function() {},// 判断是否是个functionisFunction: function( obj ) {return this.type(obj) === "function";},    //判断是否是数组isArray: Array.isArray,    //是否是window对象isWindow: function( obj ) {return obj != null && obj === obj.window;},    //检测变量是否为数字或数字字符串isNumeric: function( obj ) {return obj - parseFloat( obj ) >= 0;},    //判断参数是否纯粹的对象isPlainObject: function( obj ) {if ( this.type( obj ) !== "object" || obj.nodeType || this.isWindow( obj ) ) {return false;}// Support: Firefox <20// The try/catch suppresses exceptions thrown when attempting to access// the "constructor" property of certain host objects, ie. |window.location|// https://bugzilla.mozilla.org/show_bug.cgi?id=814622try {if ( obj.constructor &&!hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) {return false;}} catch ( e ) {return false;}// If the function hasn't returned already, we're confident that// |obj| is a plain object, created by {} or constructed with new Objectreturn true;},//判断是否是个空对象isEmptyObject: function( obj ) {var name;for ( name in obj ) {return false;}return true;},//更准确的判断确定JavaScript内置对象的类型,并返回小写形式的类型名称type: function( obj ) {if ( obj == null ) {return obj + "";}// Support: Android < 4.0, iOS < 6 (functionish RegExp)return typeof obj === "object" || typeof obj === "function" ?class2type[ toString.call(obj) ] || "object" :typeof obj;},//函数用于全局性地执行一段JavaScript代码globalEval: function( code ) {var script,indirect = eval;code = this.trim( code );if ( code ) {// If the code includes a valid, prologue position// strict mode pragma, execute code by injecting a// script tag into the document.if ( code.indexOf("use strict") === 1 ) {script = document.createElement("script");script.text = code;document.head.appendChild( script ).parentNode.removeChild( script );} else {// Otherwise, avoid the DOM node creation, insertion// and removal by using an indirect global evalindirect( code );}}},// 转换连字符式的字符串为驼峰式,用于CSS模块和数据缓存模块。例如:jQuery.camelCase( 'background-color' );camelCase: function( string ) {return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase );},    //元素节点的 nodeName 是标签名称 属性节点的 nodeName 是属性名称 文本节点的 nodeName 永远是 #text 文档节点的 nodeName 永远是 #documentnodeName: function( elem, name ) {return elem.nodeName && elem.nodeName.toLowerCase() === name.toLowerCase();},// 函数用于遍历指定的对象和数组,并以对象的每个属性(或数组的每个成员)作为上下文来遍历执行指定的函数。each: function( obj, callback, args ) {var value,i = 0,length = obj.length,isArray = isArraylike( obj );if ( args ) {if ( isArray ) {for ( ; i < length; i++ ) {value = callback.apply( obj[ i ], args );if ( value === false ) {break;}}} else {for ( i in obj ) {value = callback.apply( obj[ i ], args );if ( value === false ) {break;}}}// A special, fast, case for the most common use of each} else {if ( isArray ) {for ( ; i < length; i++ ) {value = callback.call( obj[ i ], i, obj[ i ] );if ( value === false ) {break;}}} else {for ( i in obj ) {value = callback.call( obj[ i ], i, obj[ i ] );if ( value === false ) {break;}}}}return obj;},    //去除字符串两端的空白字符。trim: function( text ) {return text == null ? "" : trim.call( text );},// 将一个类数组对象转换为真正的数组对象makeArray: function( arr, results ) {var ret = results || [];if ( arr != null ) {if ( isArraylike( Object(arr) ) ) {this.merge( ret,typeof arr === "string" ?[ arr ] : arr);} else {push.call( ret, arr );}}return ret;},    //在数组中搜索指定的值,并返回其索引值。如果数组中不存在该值,则返回 -1。inArray: function( elem, arr, i ) {return arr == null ? -1 : indexOf.call( arr, elem, i );},    //合并两个数组内容到第一个数组merge: function( first, second ) {var len = +second.length,j = 0,i = first.length;for ( ; j < len; j++ ) {first[ i++ ] = second[ j ];}first.length = i;return first;},    //使用指定的函数过滤数组中的元素,并返回过滤后的数组grep: function( elems, callback, invert ) {var callbackInverse,matches = [],i = 0,length = elems.length,callbackExpect = !invert;// Go through the array, only saving the items// that pass the validator functionfor ( ; i < length; i++ ) {callbackInverse = !callback( elems[ i ], i );if ( callbackInverse !== callbackExpect ) {matches.push( elems[ i ] );}}return matches;},// 把每个元素通过函数传递到当前匹配集合中,生成包含返回值的新的 jQuery 对象map: function( elems, callback, arg ) {var value,i = 0,length = elems.length,isArray = isArraylike( elems ),ret = [];// Go through the array, translating each of the items to their new valuesif ( isArray ) {for ( ; i < length; i++ ) {value = callback( elems[ i ], i, arg );if ( value != null ) {ret.push( value );}}// Go through every key on the object,} else {for ( i in elems ) {value = callback( elems[ i ], i, arg );if ( value != null ) {ret.push( value );}}}// Flatten any nested arraysreturn concat.apply( [], ret );},// A global GUID counter for objectsguid: 1,// 强制执行 objPerson 内的 "test" 函数的上下文:proxy: function( fn, context ) {var tmp, args, proxy;if ( typeof context === "string" ) {tmp = fn[ context ];context = fn;fn = tmp;}// Quick check to determine if target is callable, in the spec// this throws a TypeError, but we will just return undefined.if ( !this.isFunction( fn ) ) {return undefined;}// Simulated bindargs = slice.call( arguments, 2 );proxy = function() {return fn.apply( context || this, args.concat( slice.call( arguments ) ) );};// Set the guid of unique handler to the same of original handler, so it can be removedproxy.guid = fn.guid = fn.guid || this.guid++;return proxy;},now: Date.now}

0 0
原创粉丝点击