jq源碼解析之綁在$,jQuery上面的方法(實(shí)例講解)
1.當(dāng)我們用$符號(hào)直接調(diào)用的方法。在jQuery內(nèi)部是如何封裝的呢?有沒有好奇心?
// jQuery.extend 的方法 是綁定在 $ 上面的。
jQuery.extend( {
//expando 用于決定當(dāng)前頁面的唯一性。 /\D/ 非數(shù)字。其實(shí)就是去掉小數(shù)點(diǎn)。
expando: "jQuery" + ( version + Math.random() ).replace( /\D/g, "" ),
// Assume jQuery is ready without the ready module
isReady: true,
// 報(bào)錯(cuò)的情況
error: function( msg ) {
throw new Error( msg );
},
// 空函數(shù)
noop: function() {},
// 判斷是不是一個(gè)函數(shù)
isFunction: function( obj ) {
return jQuery.type( obj ) === "function";
},
//判斷當(dāng)前對(duì)象是不是window對(duì)象。
isWindow: function( obj ) {
return obj != null && obj === obj.window;
},
//判斷obj是不是一個(gè)數(shù)字 當(dāng)為一個(gè)數(shù)字字符串的時(shí)候頁可以的哦 比如 "3.2"
isNumeric: function( obj ) {
var type = jQuery.type( obj );
return ( type === "number" || type === "string" ) &&
// 這個(gè)話的意思就是要限制 "3afc 這個(gè)類型的 字符串"
!isNaN( obj - parseFloat( obj ) );
},
//判斷obj 是不是一個(gè)對(duì)象
isPlainObject: function( obj ) {
var proto, Ctor;
// obj 存在且 toString.call(obj) !== "[object object]"; 就肯定不是一個(gè)對(duì)象了。
if ( !obj || toString.call( obj ) !== "[object Object]" ) {
return false;
}
//getProto獲取原型鏈上的對(duì)象。 getProto = Object.getPrototypeOf(); 獲取原型鏈上的屬性
proto = getProto( obj );
// getProto(Object.create(null)) -> proto == null 這種情況也是對(duì)象 obj = Object.create(null);
if ( !proto ) {
return true;
}
// obj 原型上的屬性。 proto 上面有 constructor hasOwn = hasOwnPrototypeOf('name') 判斷某個(gè)對(duì)象自身是否有 這個(gè)屬性
// Ctor: 當(dāng) proto 自身有constructor的時(shí)候, 取得constructor 這個(gè)屬性的value 值。 其實(shí)就是 obj的構(gòu)造函數(shù)。 type -> function
Ctor = hasOwn.call( proto, "constructor" ) && proto.constructor;
//Ctor 類型為“function” 且 為構(gòu)造函數(shù)類型吧。 這個(gè)時(shí)候 obj 也是對(duì)象。 我的理解 這個(gè)時(shí)候,obj = new O(); 其實(shí)就是某個(gè)構(gòu)造函數(shù)的實(shí)列
return typeof Ctor === "function" && fnToString.call( Ctor ) === ObjectFunctionString;
},
//判斷obj是不是一個(gè)空對(duì)象
isEmptyObject: function( obj ) {
//var o = {}
var name;
for ( name in obj ) {
return false;
}
return true;
},
//獲取js的數(shù)據(jù)類型。 其實(shí)方法就是 Object.prototype.toString.call(xx); xx 就是要檢測的某個(gè)變量。 得到的結(jié)果是 "[object object]" "[object array]" ...
type: function( obj ) {
//除去null 和undefined 的情況。 返回本身。 也就是 null 或者 undefined. 因?yàn)?undefined == null -> true。
if ( obj == null ) {
return obj + "";
}
// 這個(gè)跟typeof xx(某個(gè)變量 ) -> undefined object,number,string,function,boolean(typeof 一個(gè)變量只能得到6中數(shù)據(jù)類型)
/**
* 1. obj 是一個(gè)對(duì)象 或者 obj 是一個(gè) function 那么 直接class2type[toString.call(obj)] 這個(gè)話其實(shí)是在class2type 中根據(jù)key值找到對(duì)應(yīng)的value。
* class2type = {
* [object object]: "object",
* [object array]:"array" ...
*
* }
* 這樣類似的值。
* class2type[toString.call(obj)] || "object" 連起來讀就是,在class2type 中找不到類型的值,就直接返回 object
*
* 2.或者返回 typeof obj。的數(shù)據(jù)類型。 -> number, string,boolean 基本數(shù)據(jù)了類型吧。 (js 中有5中基本數(shù)據(jù)類型。 null ,undefined,number,string,boolean)
*/
return typeof obj === "object" || typeof obj === "function" ?
class2type[ toString.call( obj ) ] || "object" :
typeof obj;
},
// 翻譯為:全局的Eval函數(shù)。 說句實(shí)話。沒有看懂這個(gè)是拿來干嘛的。 DOMval();
/**
*
* @param code
* function DOMEval( code, doc ) {
doc = doc || document;
var script = doc.createElement( "script" );
script.text = code;
doc.head.appendChild( script ).parentNode.removeChild( script );
}
創(chuàng)建一個(gè) script標(biāo)簽, 或remove 這個(gè)標(biāo)簽。 目前沒有搞懂拿來干嘛用。
*/
globalEval: function( code ) {
DOMEval( code );
},
// 這個(gè)是用來轉(zhuǎn)為 駝峰的用函數(shù)吧。 ms- 前綴轉(zhuǎn)為駝峰的吧。
camelCase: function( string ) {
return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase );
},
// each 方法。 $.each(obj,function(){}); 用于循環(huán)數(shù)組和對(duì)象的方法。
each: function( obj, callback ) {
var length, i = 0;
if ( isArrayLike( obj ) ) { // 當(dāng)obj 是一個(gè)數(shù)組的時(shí)候執(zhí)行這個(gè)方法
length = obj.length;
for ( ; i < length; i++ ) {
/*當(dāng)$.each(obj,function(i,item){
if( i = 2){
return false。
}
})
當(dāng)$.each(obj,function(){}) 中的匿名函數(shù)中純在 return false; 的時(shí)候跳出循環(huán)。
*/
if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
break;
}
}
} else { // for in 循環(huán)對(duì)象。 callback.call(obj[i],i,obj,[i]) === false 跟數(shù)組循環(huán)是一道理
for ( i in obj ) {
if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
break;
}
}
}
return obj;
},
// 去掉 text 兩邊的空白字符 $("input").val().trim() 一個(gè)道理吧。 text + "" 其實(shí)是為了把 text 轉(zhuǎn)成一個(gè)字符串。 類型這種情況 123.replace(rtrim,"") 是會(huì)報(bào)錯(cuò)的。
// 如果 123 + "" 其實(shí)變成了 "123"
trim: function( text ) {
return text == null ?
"" :
( text + "" ).replace( rtrim, "" );
},
// $.makeArray 其實(shí)是將類數(shù)組轉(zhuǎn)換成數(shù)組 對(duì)象。
/**
*
*
* @param arr
* @param results
* @returns {*|Array}
* 比如: var b = document.getElementsByTagName("div"); b.reverse() 。 用b 來調(diào)reserver() 方法會(huì)直接報(bào)錯(cuò)的。因?yàn)檫@個(gè)時(shí)候b是類數(shù)組對(duì)象。
* var a = $.makeArray(document.getElementsByTagName("div")); a.reverser()。 這樣就不會(huì)報(bào)錯(cuò)了。
*
*/
makeArray: function( arr, results ) {
var ret = results || [];
if ( arr != null ) {
if ( isArrayLike( Object( arr ) ) ) {
jQuery.merge( ret,
typeof arr === "string" ?
[ arr ] : arr
);
} else {
push.call( ret, arr );
}
}
return ret;
},
/**
*
* @param elem 要檢測的值
* @param arr 待處理的數(shù)組
* @param i 從待處理的數(shù)組的第幾位開始查詢. 默認(rèn)是0
* @returns {number} 返回 -1 。表示arr 中沒有該value值, 或者該值的下表
* $.inArray()。
*
*/
inArray: function( elem, arr, i ) {
//如果arr 為 null 直接返回 -1 。
/**
* 對(duì)indxOf.call(arr,elem,i);方法的解釋
* var s = new String();
* eg: var indexOf = s.indexOf; 用indexOf 變量來存字符串中的 indexOf的方法。
* indexOf.call(arr,elem,i) ; 其實(shí)就是把字符串的indexOf 繼承給數(shù)組,并且傳遞 elem 和 i 參數(shù)。
* 更簡單一點(diǎn)其實(shí)可以理解為: arr.indexOf(elem,i);
*/
return arr == null ? -1 : indexOf.call( arr, elem, i );
},
// 合并數(shù)組
/**
*
* @param first 第一個(gè)數(shù)組
* @param second 第二個(gè)數(shù)組
* @returns {*}
*/
merge: function( first, second ) {
var len = +second.length, //第二個(gè)數(shù)組的長度
j = 0, //j 從0 開始
i = first.length; //第一個(gè)數(shù)組的長度
for ( ; j < len; j++ ) {
first[ i++ ] = second[ j ];
}
// 其實(shí)用push 應(yīng)該可以吧。
first.length = i;
return first;
},
/**
*
* @param elems 帶過濾的函數(shù)
* @param callback 過濾的添加函數(shù)
* @param invert 來決定 $.grep(arr,callback) 返回來的數(shù)組,是滿足條件的還是不滿足條件的。 true 是滿足條件的。 false 是不滿足條件的。
* @returns {Array}
*
* 返回一個(gè)數(shù)組。
*/
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 function
for ( ; i < length; i++ ) {
callbackInverse = !callback( elems[ i ], i );
if ( callbackInverse !== callbackExpect ) {
matches.push( elems[ i ] );
}
}
return matches;
},
/**
*
* @param elems 帶處理的數(shù)組
* @param callback 回調(diào)函數(shù)
* @param arg 這參數(shù)用在callback回調(diào)函數(shù)的。
* callback(elems[i],i,arg)
* @returns {*}
*
* $.map(arr,function(item,i,arg){},arg)
* 將一個(gè)數(shù)組,通過callback 轉(zhuǎn)換成另一個(gè)數(shù)組。
* eg: var b = [2,3,4];
* var a = $.map(b,function(item,i,arg){
* return item + arg;
* },1)
* console.log(a) [3,4,5]
*/
map: function( elems, callback, arg ) {
var length, value,
i = 0,
ret = [];
// Go through the array, translating each of the items to their new values
if ( isArrayLike( elems ) ) {
length = elems.length;
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 arrays
return concat.apply( [], ret );
},
// 對(duì)對(duì)象的一個(gè)全局標(biāo)志量吧。 沒搞懂具體用處
guid: 1,
// Bind a function to a context, optionally partially applying any
// arguments.
/**
*
* @param fn
* @param context
* @returns {*}
*
* es6也提供了 new Proxy() 。對(duì)象。
*/
proxy: function( fn, context ) {
var tmp, args, proxy;
//當(dāng)content是字符串的時(shí)候
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 ( !jQuery.isFunction( fn ) ) {
return undefined;
}
// Simulated bind
args = 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 removed
proxy.guid = fn.guid = fn.guid || jQuery.guid++;
return proxy;
},
//$.now 當(dāng)前時(shí)間搓
now: Date.now,
// jQuery.support is not used in Core but other projects attach their
// properties to it so it needs to exist.
/**
* 檢測瀏覽器是否支持某個(gè)屬性
* $.support.style
*/
support: support
} );
以上這篇jq源碼解析之綁在$,jQuery上面的方法(實(shí)例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JQUERY的AJAX請求緩存里的數(shù)據(jù)問題處理
這篇文章主要介紹了JQUERY的AJAX請求緩存里的數(shù)據(jù)問題處理的相關(guān)資料,需要的朋友可以參考下2016-02-02
基于jquery編寫的橫向自適應(yīng)幻燈片切換特效的實(shí)例代碼
全屏自適應(yīng)jquery焦點(diǎn)圖切換特效,在IE6這個(gè)瀏覽器兼容性問題上得到了和諧,兼容IE6,適用瀏覽器:IE6、IE7、IE8、360、FireFox、Chrome、Safari、Opera、傲游、搜狗、世界之窗.相關(guān)代碼2013-08-08
兩個(gè)select之間option的互相添加操作(jquery實(shí)現(xiàn))
兩個(gè)select,將其中一個(gè)select選中的選項(xiàng)添加到另一個(gè)select中,或者點(diǎn)擊全部添加按鈕將所有的option都添加過去.2009-11-11
jQuery點(diǎn)擊tr實(shí)現(xiàn)checkbox選中的方法
jQuery點(diǎn)擊tr實(shí)現(xiàn)checkbox選中的方法,需要的朋友可以參考一下2013-03-03
解決jquery插件:TypeError:$.browser is undefined報(bào)錯(cuò)的方法
這篇文章為大家分享了一個(gè)解決jquery插件:TypeError:$.browser is undefined報(bào)錯(cuò)的方法,解決報(bào)錯(cuò)問題的方法也很簡單,需要解決此類問題的朋友不要錯(cuò)過這篇文章。2015-11-11

