Skip to content

Commit 6b6359b

Browse files
committed
trigger
1 parent 79bef9a commit 6b6359b

File tree

9 files changed

+9337
-62
lines changed

9 files changed

+9337
-62
lines changed

2.1.1-2.5.3答疑直播/demo.html

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="jQuery.1.0.2.js"></script>
9+
<script>
10+
var callList = $.callbacks("unique"); //容器 || 队列 默认once
11+
12+
var fn = function() {
13+
console.log("hello max");
14+
}
15+
callList.add(function() {
16+
console.log("hello callbacks")
17+
});
18+
callList.add(fn, fn);
19+
callList.fire();
20+
21+
22+
23+
24+
function func() {
25+
var self = {};
26+
return self;
27+
}
28+
func(); //拷贝 副本
29+
30+
console.log(func() === func())
31+
32+
var obj = func(); //拷贝 副本
33+
obj.name = "max";
34+
35+
var res = func(); //拷贝 副本
36+
console.log(res.name)
37+
38+
39+
/*
40+
41+
Deferred 延迟对象
42+
状态 成功resolve 失败reject 进行中notify
43+
回调的绑定 done fail progress
44+
45+
46+
Deferred 延迟对象 resolve => self拷贝 => fire
47+
done => self拷贝 => add
48+
49+
Deferred 延迟对象 reject => self拷贝 => fire
50+
fail => self拷贝 => add
51+
52+
Deferred 延迟对象 notify => self拷贝 => fire
53+
progress => self拷贝 => add
54+
55+
56+
var tuples = [
57+
["resolve", "done", self, "resolved"], //self 容器 add fire
58+
["reject", "fail", self, "rejected"],
59+
["notify", "progress", self]
60+
]
61+
62+
*/
63+
</script>
64+
</body>
65+
</html>
Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
/*
2+
* @Author: Administrator
3+
* @Date: 2018-10-30 20:40:51
4+
* @Last Modified by: Administrator
5+
* @Last Modified time: 2018-11-01 22:10:22
6+
*/
7+
(function(root) {
8+
var testExp = /^\s*(<[\w\W]+>)[^>]*$/;
9+
var rejectExp = /^<(\w+)\s*\/?>(?:<\/\1>|)$/;
10+
var version = "1.0.1";
11+
var optionsCache = {};
12+
var jQuery = function(selector, context) {
13+
return new jQuery.prototype.init(selector, context);
14+
}
15+
16+
jQuery.fn = jQuery.prototype = { //原型对象
17+
length: 0,
18+
jquery: version,
19+
selector: "",
20+
init: function(selector, context) {
21+
context = context || document;
22+
var match, elem, index = 0;
23+
//$() $(undefined) $(null) $(false)
24+
if (!selector) {
25+
return this;
26+
}
27+
28+
if (typeof selector === "string") {
29+
if (selector.charAt(0) === "<" && selector.charAt(selector.length - 1) === ">" && selector.length >= 3) {
30+
match = [selector]
31+
}
32+
//创建DOM
33+
if (match) {
34+
//this
35+
jQuery.merge(this, jQuery.parseHTML(selector, context));
36+
//查询DOM节点
37+
} else {
38+
elem = document.querySelectorAll(selector);
39+
var elems = Array.prototype.slice.call(elem);
40+
this.length = elems.length;
41+
for (; index < elems.length; index++) {
42+
this[index] = elems[index];
43+
}
44+
this.context = context;
45+
this.selector = selector;
46+
}
47+
} else if (selector.nodeType) {
48+
this.context = this[0] = selector;
49+
this.length = 1;
50+
return this;
51+
}
52+
53+
},
54+
css: function() {
55+
console.log("di~~didi~~")
56+
},
57+
//....
58+
}
59+
60+
jQuery.fn.init.prototype = jQuery.fn;
61+
62+
63+
jQuery.extend = jQuery.prototype.extend = function() {
64+
var target = arguments[0] || {};
65+
var length = arguments.length;
66+
var i = 1;
67+
var deep = false; //默认为浅拷贝
68+
var option;
69+
var name;
70+
var copy;
71+
var src;
72+
var copyIsArray;
73+
var clone;
74+
75+
if (typeof target === "boolean") {
76+
deep = target;
77+
target = arguments[1];
78+
i = 2;
79+
}
80+
81+
if (typeof target !== "object") {
82+
target = {};
83+
}
84+
85+
if (length == i) {
86+
target = this;
87+
i--; //0
88+
}
89+
90+
for (; i < length; i++) {
91+
if ((option = arguments[i]) !== null) {
92+
for (name in option) {
93+
src = target[name];
94+
copy = option[name];
95+
if (deep && (jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)))) {
96+
if (copyIsArray) {
97+
copyIsArray = false;
98+
clone = src && jQuery.isArray(src) ? src : [];
99+
} else {
100+
clone = src && jQuery.isPlainObject(src) ? src : {};
101+
}
102+
target[name] = jQuery.extend(deep, clone, copy);
103+
} else if (copy !== undefined) {
104+
target[name] = copy;
105+
}
106+
}
107+
}
108+
}
109+
return target;
110+
}
111+
112+
113+
jQuery.extend({
114+
//类型检测
115+
isPlainObject: function(obj) {
116+
return typeof obj === "object";
117+
},
118+
119+
isArray: function(obj) {
120+
return toString.call(obj) === "[object Array]";
121+
},
122+
123+
isFunction: function(fn) {
124+
return toString.call(fn) === "[object Function]";
125+
},
126+
//类数组转化成正真的数组
127+
markArray: function(arr, results) {
128+
var ret = results || [];
129+
if (arr != null) {
130+
jQuery.merge(ret, typeof arr === "string" ? [arr] : arr);
131+
}
132+
return ret;
133+
},
134+
135+
//合并数组
136+
merge: function(first, second) {
137+
var l = second.length,
138+
i = first.length,
139+
j = 0;
140+
141+
if (typeof l === "number") {
142+
for (; j < l; j++) {
143+
first[i++] = second[j];
144+
}
145+
} else {
146+
while (second[j] !== undefined) {
147+
first[i++] = second[j++];
148+
}
149+
}
150+
151+
first.length = i;
152+
153+
return first;
154+
},
155+
156+
parseHTML: function(data, context) {
157+
if (!data || typeof data !== "string") {
158+
return null;
159+
}
160+
//过滤掉<a> <a> => a
161+
var parse = rejectExp.exec(data);
162+
console.log(parse)
163+
return [context.createElement(parse[1])];
164+
},
165+
166+
//$.Callbacks用于管理函数队列 unique
167+
callbacks: function(options) {
168+
options = typeof options === "string" ? (optionsCache[options] || createOptions(options)) : {};
169+
var list = [];
170+
var index, length, testting, memory, start, starts;
171+
var fire = function(data) { //第二次
172+
memory = options.memory && data;
173+
index = starts || 0; //固定 1 index
174+
starts = 0;
175+
testting = true;
176+
length = list.length;
177+
for (; index < length; index++) {
178+
if (list[index].apply(data[0], data[1]) === false && options.stopOnfalse) {
179+
break;
180+
}
181+
}
182+
}
183+
var self = {
184+
add: function() {
185+
var args = Array.prototype.slice.call(arguments);
186+
start = list.length; //1
187+
args.forEach(function(fn) {
188+
if (toString.call(fn) === "[object Function]") {
189+
//检索 unique
190+
//!options.unique == false
191+
if (!options.unique || !self.has(fn)) {
192+
list.push(fn);
193+
}
194+
}
195+
});
196+
if (memory) {
197+
starts = start; //1
198+
fire(memory);
199+
}
200+
return this;
201+
},
202+
//指定上下文对象
203+
fireWith: function(context, arguments) {
204+
var args = [context, arguments];
205+
if (!options.once || !testting) {
206+
fire(args);
207+
}
208+
209+
},
210+
//参数传递
211+
fire: function() {
212+
self.fireWith(this, arguments);
213+
},
214+
has: function(fn) { //[].indexOf()
215+
//有出现 list.indexOf(fn) > -1 true 没有出现 -1 > -1 false
216+
return list.indexOf(fn) > -1;
217+
}
218+
}
219+
return self; //self
220+
},
221+
222+
// 异步回调解决方案
223+
Deferred: function(func) {
224+
var tuples = [
225+
["resolve", "done", jQuery.callbacks("once memory"), "resolved"],
226+
["reject", "fail", jQuery.callbacks("once memory"), "rejected"],
227+
["notify", "progress", jQuery.callbacks("memory")]
228+
],
229+
state = "pending",
230+
promise = {
231+
state: function() {
232+
return state;
233+
},
234+
then: function( /* fnDone, fnFail, fnProgress */ ) {
235+
var funs = [].slice.call(arguments); //多个回调函数
236+
//console.log(funs);
237+
//newDeferred
238+
return jQuery.Deferred(function(newDeferred) {
239+
tuples.forEach(function(tuple, i) {
240+
var fn = jQuery.isFunction(funs[i]) && funs[i];
241+
deferred[tuple[1]](function() {
242+
var returnDeferred = fn && fn.apply(this, arguments);
243+
if (returnDeferred && jQuery.isFunction(returnDeferred.promise)) {
244+
returnDeferred.promise()
245+
.done(newDeferred.resolve)
246+
.fail(newDeferred.reject)
247+
.progress(newDeferred.notify);
248+
}
249+
});
250+
});
251+
}).promise();
252+
},
253+
promise: function(obj) {
254+
return obj != null ? jQuery.extend(obj, promise) : promise;
255+
}
256+
},
257+
deferred = {};
258+
259+
tuples.forEach(function(tuple, i) {
260+
var list = tuple[2], //self
261+
stateString = tuple[3];
262+
263+
// promise[ done | fail | progress ] = list.add self 对象的拷贝 self.fire
264+
promise[tuple[1]] = list.add;
265+
266+
// Handle state
267+
if (stateString) {
268+
list.add(function() {
269+
// state = [ resolved | rejected ]
270+
state = stateString;
271+
});
272+
}
273+
274+
// deferred[ resolve | reject | notify ]
275+
deferred[tuple[0]] = function() {
276+
deferred[tuple[0] + "With"](this === deferred ? promise : this, arguments);
277+
return this;
278+
};
279+
deferred[tuple[0] + "With"] = list.fireWith;
280+
});
281+
282+
// Make the deferred a promise
283+
promise.promise(deferred);
284+
285+
if (func) {
286+
//newdeferred
287+
func.call(deferred, deferred);
288+
}
289+
return deferred;
290+
},
291+
//执行一个或多个对象的延迟对象的回调函数
292+
when: function(subordinate) {
293+
return subordinate.promise();
294+
},
295+
296+
});
297+
298+
function createOptions(options) {
299+
var object = optionsCache[options] = {};
300+
options.split(/\s+/).forEach(function(value) {
301+
object[value] = true;
302+
});
303+
return object;
304+
}
305+
306+
root.$ = root.jQuery = jQuery;
307+
})(this);

0 commit comments

Comments
 (0)