-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
166 lines (142 loc) · 4.11 KB
/
Copy pathindex.html
File metadata and controls
166 lines (142 loc) · 4.11 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/main.js"></script>
</body>
<script>
; ((root) => {
// 构造函数需要接收处理的数据
// 并将它挂载到this上,这里的this是实例对象
function _(value) {
if (!(this instanceof _)) {
return new _(value)
}
this._wrapped = value;
}
// 静态方法map
_.map = function (arr, callback) {
let result = []
for (let i = 0; i < arr.length; i++) {
result.push(callback(arr[i], i))
}
return result
}
// 链式调用
_.chain = function (obj) {
var instance = _(obj);
instance._chain = true;
return instance;
};
// 静态方法filter
_.filter = function (arr, callback) {
let result = []
for (let i = 0; i < arr.length; i++) {
if (callback(arr[i], i)) {
result.push(arr[i])
}
}
return result
}
// 静态方法unique
_.unique = function (arr) {
let result = []
for (let i = 0; i < arr.length; i++) {
if (result.indexOf(arr[i]) === -1) {
result.push(arr[i])
}
}
return result
}
//静态方法each(辅助函数)
_.each = function (array, callback) {
var length = array.length;
for (var i = 0; i < length; i++) {
callback(array[i]);
}
}
// 静态方法functions(辅助函数)
_.functions = function (obj) {
var result = [];
for (var key in obj) {
if (typeof obj[key] === 'function') {
result.push(key);
}
}
return result;
}
_.prototype.value = function () {
return this._wrapped;
}
// 将静态方法映射到成实例方法
_.mixin = function (obj) {
// 遍历obj里面的函数属性
_.each(_.functions(obj), function (name) {
// 取出每个函数
var func = _[name] = obj[name];
// 在原型上设置一个同名函数
_.prototype[name] = function () {
// 注意这里,实例方法待处理数据是构造函数接收的参数,改造构造函数的代码在后面
// 这里将数据取出来作为静态方法的第一个参数
var args = [this._wrapped];
// 将数据和其他参数放到一个数组里面,作为静态方法的参数
Array.prototype.push.apply(args, arguments);
console.log('args--->', args);
// 用处理好的参数来调用静态方法
/*
_([1,2,3]).map( item => item * 2 )
相当于
_.map([1, 2, 3], (item) => (item * 2))
*/
const res = func.apply(_, args);
// 如果是链式调用,就返回实例对象
if (this._chain) {
// return _(res);
this._wrapped = res;
return this;
}
// 将结果返回
return res
};
});
return _;
};
// 将_自己作为参数传进去
_.mixin(_);
root._ = _;
})(this);
// 测试
// let arr = [1, 2, 3, 4, 5]
// let result = _.map(arr, (item, index) => {
// return item * 2
// })
// const result = _(arr).map(item => item * 2)
// console.log(result) // [2, 4, 6, 8, 10]
// mixin 顺带支持插件
/* _.mixin({
add: function (a, b) {
return a + b
},
capitalize: function (string) {
return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
}
})
const result = _(1).add(2)
console.log(result)
console.log(_("fabio").capitalize()) */
// 支持链式调用
// 实例方法
let arr = [1, 1, 2, 2, 3, 3, 4, 5]
const result = _(arr).chain().map(item => item * 2).unique().value()
console.log(result) // [2, 4, 6, 8, 10]
// 静态方法
// const result = _.chain(arr).map(item => item * 2).unique().value()
// console.log(result)
</script>
</html>