Skip to content

Commit 4195e25

Browse files
插件封装
1 parent 752aadc commit 4195e25

File tree

87 files changed

+1335
-29401
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+1335
-29401
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
<script src="jquery-2.2.0.js"></script>
7+
</head>
8+
<body>
9+
<script>
10+
function jQuery() {
11+
12+
}
13+
jQuery.fn = jQuery.prototype = {};
14+
15+
16+
// 添加extend方法
17+
/*
18+
* 需求:
19+
* 1、传入一个参数,谁调用就给谁混入内容
20+
* 2、传入多个参数,把后面对象的内容混入到第一个对象中
21+
* */
22+
jQuery.extend = jQuery.fn.extend = function() {
23+
24+
// 被混入的目标,默认为第一个参数
25+
var target = arguments[ 0 ];
26+
27+
// 传入一个参数,把这个参数的内容混入到this中
28+
if( arguments.length === 1 ) {
29+
30+
// 传入一个参数,目标改为this
31+
target = this;
32+
33+
// 给this混入内容
34+
for( var key in arguments[ 0 ] ) {
35+
target[ key ] = arguments[ 0 ][ key ];
36+
}
37+
}
38+
39+
// 传入多个参数,把后面对象的内容混入到第一个对象中
40+
else if( arguments.length >= 2 ) {
41+
42+
// 遍历得到后面所有的对象
43+
for( var i = 1, len = arguments.length; i < len; i++ ) {
44+
45+
// 遍历每一个对象所有的属性
46+
for( var key in arguments[ i ] ) {
47+
48+
// 把后面对象的内容混入到第一个对象中
49+
target[ key ] = arguments[ i ][ key ];
50+
}
51+
}
52+
}
53+
54+
// 给谁混入返回谁
55+
return target;
56+
}
57+
58+
// 添加extend方法
59+
/*
60+
* 需求:
61+
* 1、传入一个参数,谁调用就给谁混入内容
62+
* 2、传入多个参数,把后面对象的内容混入到第一个对象中
63+
* */
64+
jQuery._extend = jQuery.fn._extend = function() {
65+
66+
var i = 1, key,
67+
arg = arguments,
68+
target = arg[ 0 ],
69+
argLen = arg.length;
70+
71+
// 参数个数为1时,目标改为this,起始i改为0
72+
if( argLen === 1 ) {
73+
target = this;
74+
i = 0;
75+
}
76+
77+
// 遍历得到arguments中的对象
78+
for( ; i < argLen; i++ ) {
79+
80+
// 遍历每一个对象所有的属性
81+
for( key in arg[ i ] ) {
82+
target[ key ] = arg[ i ][ key ];
83+
}
84+
}
85+
86+
// 给谁混入返回谁
87+
return target;
88+
}
89+
</script>
90+
<script>
91+
/*console.log(jQuery.fn._extend({
92+
val: 111,
93+
value: 999
94+
}));
95+
console.log(jQuery.fn);*/
96+
97+
var obj = {};
98+
jQuery._extend(obj, {
99+
val1: 1111
100+
}, {
101+
val2: 2222
102+
}, {
103+
val3: 3333
104+
});
105+
console.log( obj );
106+
</script>
107+
</body>
108+
</html>
File renamed without changes.

0 commit comments

Comments
 (0)