上一篇:jQuery 原理的模擬代碼 -1 核心部分
在 jQuery 中,可以對每一個 DOM 對象保存私有的數據。
這個數據當然要通過屬性來進行存取,但是,有多個屬性怎么辦呢?,要定義多個屬性嗎?,屬性的名字叫什么呢?會不會與其他的屬性有沖突呢?
在 jQuery 中,針對 DOM 對象擴展的私有數據可以用一個對象來表示,多個數據就使用這個對象的多個屬性來表示。為了能夠通過 DOM 對象找到這個擴展數據對象,而不會與其他現有的屬性沖突,在 jQuery 中通過 expando 這個常量表示擴展對象的屬性名,這個 expando 的值是計算出來的。而這個屬性的值就是用來找到擴展對象的鍵值。
例如,我們可以定義 expando 的值為 "jQuery1234" ,那么,我們可以為每個 DOM 對象增加這個名為 "jQuery1234" 的屬性,這個屬性的值可以是一個鍵,例如為 1000。
在 jQuery 對象上的 cache 用來保存所有對象擴展的對象,這個對象可以看作一個字典,屬性名就是鍵值,所對應的值就是擴展數據對象。
也就是說,在 jQuery 對象的 cache 上,將會有一個 1000 的成員,這個成員引用的對象就是 1000 號 DOM 對象的私有擴展對象。1000 號成員的私有數據將被存在在這個對象上。
當一個 DOM 對象需要取得擴展數據的時候,首先通過對象的 expando 屬性取得一個鍵值,然后通過這個鍵值到 jQuery.cache 中取得自己的擴展對象,然后在擴展對象上讀寫數據。
1 /// <reference path="jQuery-core.js" />
2
3 // 常用方法
4 function now() {
5 return (new Date).getTime();
6 }
7
8 // 擴充數據的屬性名,動態生成,避免與已有的屬性沖突
9 var expando = "jQuery" + now(), uuid = 0, windowData = {};
10 jQuery.cache = {};
11 jQuery.expando = expando;
12
13 // 數據管理,可以針對 DOM 對象保存私有的數據,可以讀取保存的數據
14 jQuery.fn.data = function (key, value) {
15
16 // 讀取
17 if (value === undefined) {
18 return jQuery.data(this[0], key);
19 }
20 else { // 設置
21
22 this.each(
23 function () {
24 jQuery.data(this, key, value);
25 }
26 );
27 }
28 }
29 // 移除數據,刪除保存在對象上的數據
30 jQuery.fn.removeData = function (key) {
31 return this.each(function () {
32 jQuery.removeData(this, key);
33 })
34 }
35
36
37 // 為元素保存數據
38 jQuery.data = function (elem, name, data) { // #1001
39
40 // 取得元素保存數據的鍵值
41 var id = elem[expando], cache = jQuery.cache, thisCache;
42
43 // 沒有 id 的情況下,無法取值
44 if (!id && typeof name === "string" && data === undefined) {
45 return null;
46 }
47
48 // Compute a unique ID for the element
49 // 為元素計算一個唯一的鍵值
50 if (!id) {
51 id = ++uuid;
52 }
53
54 // 如果沒有保存過
55 if (!cache[id]) {
56 elem[expando] = id; // 在元素上保存鍵值
57 cache[id] = {}; // 在 cache 上創建一個對象保存元素對應的值
58 }
59
60 // 取得此元素的數據對象
61 thisCache = cache[id];
62
63 // Prevent overriding the named cache with undefined values
64 // 保存值
65 if (data !== undefined) {
66 thisCache[name] = data;
67 }
68
69 // 返回對應的值
70 return typeof name === "string" ? thisCache[name] : thisCache;
71
72 }
73
74 // 刪除保存的數據
75 jQuery.removeData = function (elem, name) { // #1042
76
77 var id = elem[expando], cache = jQuery.cache, thisCache = cache[id];
78
79 // If we want to remove a specific section of the element's data
80 if (name) {
81 if (thisCache) {
82 // Remove the section of cache data
83 delete thisCache[name];
84
85 // If we've removed all the data, remove the element's cache
86 if (jQuery.isEmptyObject(thisCache)) {
87 jQuery.removeData(elem);
88 }
89 }
90
91 // Otherwise, we want to remove all of the element's data
92 } else {
93
94 delete elem[jQuery.expando];
95
96 // Completely remove the data cache
97 delete cache[id];
98 }
99 }
100
101 // 檢查對象是否是空的
102 jQuery.isEmptyObject = function (obj) {
103 // 遍歷元素的屬性,只有要屬性就返回假,否則返回真
104 for (var name in obj) {
105 return false;
106 }
107 return true;
108
109 }
110
111 // 檢查是否是一個函數
112 jQuery.isFunction = function (obj) {
113 var s = toString.call(obj);
114 return toString.call(obj) === "[object Function]";
115 }
116
下面的腳本可以保存或者讀取對象的擴展數據。
1 // 數據操作
2 $("#msg").data("name", "Hello, world.");
3 alert($("#msg").data("name"));
4 $("#msg").removeData("name");
5 alert($("#msg").data("name"));
原文:http://www.cnblogs.com/haogj/archive/2010/08/01/1789715.html