-
Notifications
You must be signed in to change notification settings - Fork 0
/
how jquery works.html
234 lines (192 loc) · 6.82 KB
/
how jquery works.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<!DOCTYPE html>
<html>
<head>
<title>How Jquery Works</title>
<link rel="stylesheet" type="text/css" href="./CSS/home.css">
<style></style>
</head>
<body>
<H1>How Jquery Works</H1>
<div id="container"></div>
<div id="footer">
<h3> Document Information </h3>
<ul>
<li>
Date: 2015/05/12
</li>
<li>
Reference:
<ul>
<li><a target="_blank" href="http://www.codeproject.com/Articles/426013/How-jQuery-works">How Jquery works</a></li>
<li><a target="_blank" href="http://mag.splashnology.com/article/the-structure-of-jquery-dive-into-source-code/2517/">the structure of jquery</a></li>
<li><a target="_blank" href="http://nuysoft.iteye.com/blog/1177451">jquery source code</a></li>
<li><a target="_blank" href="http://www.cnblogs.com/aaronjs/p/3279314.html">jquery source code2</a></li>
<li><a target="_blank" href="http://blog.quickui.org/2012/06/07/jquery-fn-init/">jQuery fn init</a></li>
<li><a target="_blank" href="http://mag.splashnology.com/article/the-structure-of-jquery-dive-into-source-code/2517/">The structure of jQuery</a></li>
</ul>
</li>
</ul>
</div>
</body>
</html>
<script src="./LIB/jquery/jquery-2.1.3.js"></script>
<script src="./LIB/home.js"></script>
<script type="text/javascript">
/*
JavaScript Constructor
*/
//This constructor will implicitly return 'this';
var normalConstructor = function(name){
this.name = name;
};
//Do this can avoid global object pollution
var betterConstructor = function(age){
if(this === window){
return new betterConstructor(age);
}
this.age = age;
};
//jQuery constructor: factory pattern
function jQueryConstructor() {
function Init() {
this.name = 'ccc';
return this;
}
Init.prototype = jQueryConstructor.prototype;
Init.prototype.fn1 = function() {};
Init.prototype.fn2 = function() {};
var kobj = new Init();
return kobj;
}
var ins1 = new normalConstructor('Jim');
console.log(window.name);
var ins2 = normalConstructor('Jim');
console.log(window.name); //global object polluted.
var ins3 = new betterConstructor('8');
console.log(window.age);
var ins4 = betterConstructor('8');
console.log(window.age);
var a1 = jQueryConstructor();
var a2 = new jQueryConstructor();
console.log(a1 instanceof jQueryConstructor);
console.log(a2 instanceof jQueryConstructor);
/*
myQuery and private init function
*/
var myQueryWithoutInit = (function (){
// Define a local copy of 'k'
function init(){
return this;
};
var k = function (selector, context)
{
// The k object is actually just the init constructor 'enhanced'
var kobj = new init();
return kobj;
};
//Define k’s fn prototype, specially contains init method
// Give the init function the 'k' prototype for later instantiation
k.fn = k.prototype = init.prototype = {};
k.fn.fn1 = function(){
console.log("fn1");
};
k.fn.fn2 = function(){
console.log("fn2");
};
// Return 'k' to the global object
return k;
})();
/*
myQuery and init function is under myQuery.
*/
var myQueryWithInit = (function (){
// Define a local copy of 'k'
var k = function (selector, context)
{
// The k object is actually just the init constructor 'enhanced'
var kobj = new k.fn.init(selector, context);
return kobj;
};
//Define k’s fn prototype, specially contains init method
k.fn = k.prototype = {};
k.fn.init = function (selector, context){
if (!selector)
{
return this;
}
};
// Give the init function the 'k' prototype for later instantiation
k.fn.init.prototype = k.fn;
k.fn.fn1 = function(){
console.log("fn1");
};
k.fn.fn2 = function(){
console.log("fn2");
};
// Return 'k' to the global object
return k;
})();
var myQueryOutside = {};
(function (){
function init(){
return this;
};
myQueryOutside = function (selector, context)
{
var kobj = new init();
return kobj;
};
myQueryOutside.fn = myQueryOutside.prototype = init.prototype = {};
myQueryOutside.fn.fn1 = function(){
console.log("fn1");
};
myQueryOutside.fn.fn2 = function(){
console.log("fn2");
};
})();
/*
old jQuery 1.1 version init like this
*/
var oldQuery = (function(){
var k = function(){
if(this === window){
return new k();
}
}
//Define k’s fn prototype, specially contains init method
k.fn = k.prototype = {};
k.fn.fn1 = function(){
console.log("fn1");
};
k.fn.fn2 = function(){
console.log("fn2");
};
return k;
})();
/*
Demo
*/
var oldQueryIns1 = oldQuery();
var oldQueryIns2 = new oldQuery(); //Ins1 and Ins2 will be no difference
var myQueryWithInitIns1 = myQueryWithInit();
var myQueryWithoutInitIns1 = myQueryWithoutInit();
var myQueryOutsideIns1 = myQueryOutside();
//plugin function : plugin will appear in all the entities that already created and those that will be created. Adding properties directly to the object, we implement the static properties.
myQueryWithInit.fn.fn3 = function(){
console.log("fn3");
};
myQueryWithoutInit.fn3 = function(){
console.log("fn3");
};
myQueryOutside.fn3 = function(){
console.log("fn3");
};
var myQueryWithInitIns2 = myQueryWithInit();
var myQueryWithoutInitIns2 = myQueryWithoutInit();
console.log("myQueryWithInitIns1.init === undefined : " + (myQueryWithInitIns1.init === undefined).toString());
console.log('myQueryWithoutInitIns1.init === undefined : ' + (myQueryWithoutInitIns1.init === undefined).toString());
console.log('myQueryWithInitIns1.fn3 === undefined : ' + (myQueryWithInitIns1.fn3 === undefined).toString());
console.log('myQueryWithoutInitIns1.fn3 === undefined : ' + (myQueryWithoutInitIns1.fn3 === undefined).toString());
console.log('myQueryWithInitIns2.fn3 === undefined : ' + (myQueryWithInitIns2.fn3 === undefined).toString());
console.log('myQueryWithoutInitIns2.fn3 === undefined : ' + (myQueryWithoutInitIns2.fn3 === undefined).toString());
</script>