標籤:style blog http io ar os 使用 sp for
jquery1.4 jquery1.4下載
這裡使用了 jQuery1.4,為什麼使用 1.4 因為 1.4 很多特性沒有添加分析起來相對容易。
這個 data 的實現是擴充在 jQuery 靜態函數裡面的,我們平常這樣( $(‘#data‘).data(‘tudou‘, ‘abc‘) )調用的是 jQuery 原型上的 data ,原型上面的 data 再調用下面 jQuery 靜態 data 方法實現。
jQuery.extend({cache: {},expando:expando,// The following elements throw uncatchable exceptions if you// attempt to add expando properties to them.noData: {"embed": true,"object": true,"applet": true},data: function( elem, name, data ) { // 不是正確的元素過濾掉if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {return;}elem = elem == window ?windowData :elem;var id = elem[ expando ], cache = jQuery.cache, thisCache;// Handle the case where there‘s no name immediately // 沒有 data 名 且 也沒有設定過 data 返回if ( !name && !id ) {return null;}// Compute a unique ID for the elementif ( !id ) { id = ++uuid;}// Avoid generating a new cache unless none exists and we// want to manipulate it.if ( typeof name === "object" ) {elem[ expando ] = id; // 如果傳遞過來的是對象,直接擴充到 cache 對象中thisCache = cache[ id ] = jQuery.extend(true, {}, name);} else if ( cache[ id ] ) { // 如果有儲存過,則拿到以前的thisCache = cache[ id ];} else if ( typeof data === "undefined" ) {thisCache = emptyObject;} else { // 第一次儲存,要建立一個Null 物件thisCache = cache[ id ] = {};}// Prevent overriding the named cache with undefined values // 把資料寫入 cache 儲存 根據uuidif ( data !== undefined ) {elem[ expando ] = id;thisCache[ name ] = data;} // 返回附加的資料return typeof name === "string" ? thisCache[ name ] : thisCache;},removeData: function( elem, name ) {if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {return;}elem = elem == window ?windowData :elem;var id = elem[ expando ], cache = jQuery.cache, thisCache = cache[ id ];// If we want to remove a specific section of the element‘s dataif ( name ) {if ( thisCache ) {// Remove the section of cache datadelete thisCache[ name ];// If we‘ve removed all the data, remove the element‘s cacheif ( jQuery.isEmptyObject(thisCache) ) {jQuery.removeData( elem );}}// Otherwise, we want to remove all of the element‘s data} else {// Clean up the element expandotry {delete elem[ expando ];} catch( e ) {// IE has trouble directly removing the expando// but it‘s ok with using removeAttributeif ( elem.removeAttribute ) {elem.removeAttribute( expando );}}// Completely remove the data cachedelete cache[ id ];}}});
這個 data 的實現是在 jQuery 函數上建立了一個靜態變數 cache 對象,key 就是 data 的 name, value 就是 data 的 value。如:
現在來看一張圖,來觀察 data 實現的方法
實現方法就是 jQuery 在對元素操作的時候會賦值一個 "jQuery" + now() 時間戳記的屬性,其值是一個 uuid 唯一的數字,每次操作會加 1 ,然後在 jQuery 的 cache 的對象靜態變數中根據這個 uuid 賦值一個 key 就像這樣 {5:{}}。並且把你要加入的 data 放去這個 key 為 5 的對象中 {5:{tudou:"abc"}}。他們唯一關聯的就是這個 uuid 。
看看 jQuery 的原型實現
jQuery.fn.extend({data: function( key, value ) {if ( typeof key === "undefined" && this.length ) {return jQuery.data( this[0] );} else if ( typeof key === "object" ) { // 對象直接進行 data 儲存return this.each(function() {jQuery.data( this, key );});}var parts = key.split(".");parts[1] = parts[1] ? "." + parts[1] : ""; // 如果只傳 name 就是擷取值if ( value === undefined ) { // 設定會觸發 getData 自訂方法 他們都可以通過 bind 捕獲var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);if ( data === undefined && this.length ) {data = jQuery.data( this[0], key );}return data === undefined && parts[1] ?this.data( parts[0] ) :data;} else { // 設定值會觸發 setData 方法 他們都可以通過 bind 捕獲return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function() {jQuery.data( this, key, value );});}},removeData: function( key ) {return this.each(function() {jQuery.removeData( this, key );});}});
這裡的實現就顯得輕鬆許多了,只需要多 參數、擷取/設定 值進行處理就可以了 ,然後去調用對應的 jQuery.data 方法去實現值的設定或者擷取
之前簡單的類比過一個 原理差不多是這樣
proxy 比較簡單,但是很容易暈的一個東西,給你來幾個 proxy + 返回閉包 基本都會暈 :(
proxy: function( fn, proxy, thisObject ) {if ( arguments.length === 2 ) {if ( typeof proxy === "string" ) { // 第一種方式調用方式 $.proxy(obj, ‘fn‘)thisObject = fn;fn = thisObject[ proxy ];proxy = undefined;} else if ( proxy && !jQuery.isFunction( proxy ) ) { // 第二種調用方式 $.proxy(obj.fn, obj)thisObject = proxy;proxy = undefined;}}if ( !proxy && fn ) { /* * 返回重構好 this 的函數, 那麼這裡的 thisObject 是指向了最終 this 要指向的地方 */proxy = function() {return fn.apply( thisObject || this, arguments );};}// Set the guid of unique handler to the same of original handler, so it can be removedif ( fn ) {proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;}// So proxy can be declared as an argumentreturn proxy;}
值得關注的就是 這個 thisObject 因為 js 中 函數、對象、數組是傳址的,所以 thisObject 能指向你要代理到那個對象的 this 中。感覺非常繞,但是搞明白也就沒啥了。隨便說下 thisObject 肯定是一個閉包了
var obj = { name: ‘tudousi‘, test: function() { alert( this.name ); return false; } }; var func = $.proxy(obj, ‘test‘); $(‘.show‘).click(func);
jQuery剝皮三- data、proxy、event