-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_prototype_func.js
178 lines (152 loc) · 3.8 KB
/
array_prototype_func.js
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
"use strict"
// ==== Native Filter ====
Array.prototype.filter = function(fn) {
let res = [];
let _arr = this;
for (let i = 0; i < _arr.length; ++i) {
if ( fn.call(this, _arr[i], i, _arr) ) {
res.push(_arr[i]);
}
}
return res;
};
let filter_words = ['a', 'ab', 'bcd', 'asdf', 'asdfre', 'qwerre'];
let new_result = filter_words.filter(word => word.length > 3);
console.log('Filter: ', filter_words, "=>", new_result);
// Filter: [ 'a', 'ab', 'bcd', 'asdf', 'asdfre', 'qwerre' ] => [ 'asdf', 'asdfre', 'qwerre' ]
// ==== Native Map ====
Array.prototype.map = function(fn) {
let res = [];
let _arr = this;
for (let i = 0; i < _arr.length; ++i) {
res.push( fn.call(this, _arr[i], i, _arr) );
}
return res;
};
let map_array = [1, 4, 9, 16];
const new_map = map_array.map(x => x * 2);
console.log("Map:", map_array, "=>", new_map);
// Map: [ 1, 4, 9, 16 ] => [ 2, 8, 18, 32 ]
// ==== Native Reduce ====
Array.prototype.reduce = function(fn) {
let _arr = this;
let accum = _arr[0];
for (let i = 1; i < _arr.length; ++i) {
accum = fn(accum, _arr[i]);
}
return accum;
};
const reduce_array = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
console.log("Reduce:", reduce_array, "=>", reduce_array.reduce(reducer));
// Reduce: [ 1, 2, 3, 4 ] => 10
// ==== Native Debounce ====
function debounce(fn, wait) {
let timeout_id;
return function() {
let context = this;
let args = arguments;
clearTimeout(timeout_id);
timeout_id = setTimeout(() => {
fn.apply(context,args);
}, wait);
};
}
// let event = debounce(function() { console.log('Debounce Outputs'); }, 2000);
// event();
// event();
// event();
// ==== Native Throttle ====
function throttle(fn, limit) {
let flag;
let prev;
let timeout_id;
return function() {
let context = this;
let args = arguments;
if (!flag) {
fn.apply(context, args);
flag = true;
prev = Date.now();
timeout_id = setTimeout(() => flag = false, limit);
} else {
clearTimeout(timeout_id);
timeout_id = setTimeout(() => {
flag = false;
fn.apply(context, args);
}, limit - (Date.now() - prev));
}
};
}
// let event2 = throttle(function() { console.log('Thrrottle Outputs'); }, 1000);
// for (let i = 0; i < 1000; ++i) {
// event2();
// }
// ==== Native Bind ====
Function.prototype.bind = function() {
let func = this;
let context = arguments[0]; // new target ( obj )
let prev_args = [].slice.call(arguments, 1); // previous arguments ( func: function(...args) )
return function() {
let cur_args = [].slice.call(arguments); // current arguments ( newf(...args) )
let new_args = prev_args.concat(cur_args);
return func.apply(context, new_args);
};
};
// test case 1
let test = {
v: "Michael",
func: function() { // prev args
console.log(this.v);
}
};
test.func();
// Michael
let obj = {v: "Abby"};
let newf = test.func.bind(obj);
newf(); // current args
// Abby
// test case 2
let func = function(a, b) {
return a + b
};
let boundFunc = func.bind(null, 'foo');
let result = boundFunc('aa', 'aa');
console.log(result);
// fooaa
// ==== printTasks ====
function _showTime() {
return (new Date()).toLocaleTimeString();
}
function printTasks(list) {
if (list.length === 1) {
setTimeout(() => console.log(_showTime(), "=>", list[0]["Value"]), list[0]["Time"]);
return ;
}
setTimeout(() => {
console.log(_showTime(), "=>", list[0]["Value"]);
printTasks( list.slice(1) );
}, list[0]["Time"]);
}
let input = [
{
Value: "a",
Time: 2000
},
{
Value: "b",
Time: 1000
},
{
Value: "c",
Time: 3000
},
];
console.log(_showTime(), "printTasks Starts");
printTasks(input);
/*
5:27:42 PM printTasks Starts
5:27:44 PM => a
5:27:45 PM => b
5:27:48 PM => c
*/