//null Object.prototype.toString.call(null);//」[object Null]」 //undefined Object.prototype.toString.call(undefined);//」[object Undefined]」 //string Object.prototype.toString.call(「aaa」);//」[object String]」 //number Object.prototype.toString.call(111);//」[object Number]」 //boolean Object.prototype.toString.call(true);//」[object Boolean]」 //函數 Function fn(){console.log(「xixi」);} Object.prototype.toString.call(fn);//」[object Function]」 //數組類型 var arr = [1,2,,3,4]; Object.prototype.toString.call(arr);//」[object Array]」 //日期 var date = new Date(); Object.prototype.toString.call(date);//」[object Date]」 //自定義類型 //不能判斷aa是否是AA的實例,要用instanceof判斷 function AA(a, b) { this.a = a; this.b = b; } var aa = new AA("xixi", 'haha'); Object.prototype.toString.call(aa); //」[object Object]」 Object.prototype.toString.call(aa); //」[object Object]」 //正則 var aa = /^\w$/ Object.prototype.toString.call(aa); //」[object RegExp]」
class2type
,並取出對象函數自帶的toString
方法,即爲Object.prototype.toString.call()
class2type = {}, toString = class2type.toString,
而後填充class2type
的值;javascript
$.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function(i, name) { class2type["[object " + name + "]"] = name.toLowerCase() //最終結果 //class2type[」[object Boolean]」] = boolean, //class2type[」[object Number]」] = number, //class2type[」[object String]」] = string, // ... })
準備工做作完,就能夠進行對象的判斷了;java
//好比,若是要判斷function aa(){};則返回class2type[Object.prototype.toString.call(aa)]==class2type[」[object Function]」]==function;因此就判斷出爲函數 function type(obj) { //若是obj爲null或者undefined,返回字符串'null','undefined';不然返回class2type[]或者object return obj == null ? String(obj) :class2type[toString.call(obj)] || "object" }
$.type = type
下面根據上面的封裝方法返回值判斷類型;node
function isFunction(value) { return type(value) == "function" }
$.isFunction = isFunction
function isWindow(obj) { return obj != null && obj == obj.window }
$.isWindow = isWindow
function isDocument(obj) { return obj != null && obj.nodeType == obj.DOCUMENT_NODE }
function isObject(obj) { return type(obj) == "object" }
function isPlainObject(obj) { return isObject(obj) && !isWindow(obj) && obj.__proto__ == Object.prototype }
$.isPlainObject = isPlainObject
function isArray(value) { return value instanceof Array }
$.isArray = isArray
function likeArray(obj) { return typeof obj.length == 'number' }
$.isEmptyObject = function(obj) { var name for (name in obj) return false return true }
$.isNumeric = function(val) { var num = Number(val), type = typeof val return val != null && type != 'boolean' && (type != 'string' || val.length) && !isNaN(num) && isFinite(num) || false }
camelize = function(str){ return str.replace(/-+(.)?/g, function(match, chr){ return chr ? chr.toUpperCase() : '' } ) }
$.camelCase = camelize
java $.camelCase('hello-there') //=> "helloThere" $.camelCase('helloThere') //=> "helloThere"
str.replcace(a,b)
json
將str中的a替換成b;上面代碼中將b用了函數返回值來表達;數組
//爲了判斷某個節點是否是另外一個節點的後代,瀏覽器引入了contains()方法; $.contains = document.documentElement.contains ? //若是瀏覽器支持contains()方法,就執行這個函數 function(parent, node) { return parent !== node && parent.contains(node) } : //不然循環判斷 function(parent, node) { while (node && (node = node.parentNode)) if (node === parent) return true return false }
false
。$.each = function(elements, callback){ var i, key if (likeArray(elements)) { for (i = 0; i < elements.length; i++) if (callback.call(elements[i], i, elements[i]) === false) return elements } else { for (key in elements) if (callback.call(elements[key], key, elements[key]) === false) return elements } return elements }
function extend(target, source, deep) { for (key in source) //若是是神拷貝,source[key]是對象或者數組 if (deep && (isPlainObject(source[key]) || isArray(source[key]))) { //source[key]是對象,而target[key]不是對象 if (isPlainObject(source[key]) && !isPlainObject(target[key])) target[key] = {} //source[key]是數組,而target[key]不是數組 if (isArray(source[key]) && !isArray(target[key])) target[key] = [] //遞歸 extend(target[key], source[key], deep) } else if (source[key] !== undefined) target[key] = source[key] }
$.extend = function(target){ var deep, args = slice.call(arguments, 1) //若是傳入的第一個參數爲布爾值 if (typeof target == 'boolean') { deep = target //將除第一個參數外的參數賦值給target target = args.shift() } //遍歷參數,將參數都複製到target上; args.forEach(function(arg){ extend(target, arg, deep) }) return target }
$.extend(target, [source, [source2, ...]]) ⇒ target $.extend(true, target, [source, ...]) ⇒ target v1.0+
var target = { one: 'patridge' }, source = { two: 'turtle doves' } $.extend(target, source)//{one: "patridge", two: "turtle doves"}
target
表明被拓展的對象,source
爲源對象;deep
表明是深複製仍是淺複製;瀏覽器
slice.call(arguments, 1)
選取傳入參數的第一個參數到最後一個參數;app
$.inArray = function(elem, array, i){ return emptyArray.indexOf.call(array, elem, i) }
$.inArray("abc",["bcd","abc","edf","aaa"]);//=>1 $.inArray("abc",["bcd","abc","edf","aaa"],1);//=>1 $.inArray("abc",["bcd","abc","edf","aaa"],2);//=>-1
$.map = function(elements, callback){ var value, values = [], i, key if (likeArray(elements)) for (i = 0; i < elements.length; i++) { value = callback(elements[i], i) if (value != null) values.push(value) } else for (key in elements) { value = callback(elements[key], key) if (value != null) values.push(value) } return flatten(values) }
element
爲類數組對象或者對象;dom
flatten()
函數
function flatten(array) { return array.length > 0 ? $.fn.concat.apply([], array) : array }
oop
這裏巧妙應用了apply
方法,apply
方法的第一個元素會被看成this
,第二個元素被作爲傳入的參數arguments
;
例如:concat.apply([],[[1],[2],[3]]),
等價於[].concat([1],[2],[3])
,輸出的值爲[1,2,3]
,就實現了數組的扁平化;
$.noop = function() {}
if (window.JSON) $.parseJSON = JSON.parse
$.trim = function(str) { return str == null ? "" : String.prototype.trim.call(str) }