-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
183 lines (170 loc) · 3.77 KB
/
demo.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
179
180
181
182
183
// 深拷贝1
function deepClone(obj, hash = new WeakMap()){
if(obj instanceof RegExp) return new RegExp(obj)
if(obj instanceof Date) return new Date(obj)
if(obj === null || typeof obj !== 'object') return obj
if(hash.has(obj)) return hash.get(obj)
let t = new obj.constructor()
hash.set(obj,t)
for(let key in obj){
if(obj.hasOwnProperty(key)){
t[key] = deepClone(obj[key], hash)
}
}
return t
}
// 深拷贝2
function deepClone(obj){
if(obj instanceof RegExp) return new RegExp(obj)
if(obj instanceof Date) return new Date(obj)
if(obj === null || typeof obj !== 'object') return obj
let newObj = Array.isArray(obj) ? [] : {}
for(let key in obj){
if(obj.hasOwnProperty(key)){
if(typeof obj[key] === 'object'){
newObj[key] = deepClone(obj[key])
}else{
newObj[key] = obj[key]
}
}
}
return newObj
}
// 模拟new
function _new(constructor, ...args){
let obj = Object.create(constructor.prototype)
let res = constructor.apply(obj, args)
return res !== null && typeof res === 'object' ? obj : res
}
// 防抖
function debounce(func, delay, isImmediate){
let timer = null
if(isImmediate){
let flag = true
return function(){
clearTimeout(timer)
if(flag){
func.apply(this,arguments)
flag = false
}else{
timer = setTimeout(()=>{
flag = true
}, delay)
}
}
}
return function(){
if(timer) clearTimeout(timer)
timer = setTimeout(()=>{
func.apply(this, arguments)
}, delay)
}
}
// 节流
function throttle(func, delay, isImmediate){
let timer = null
if(isImmediate){
let flag = true
if(!timer){
if(flag){
func.apply(this,arguments)
flag = false
}
timer = setTimeout(() => {
flag = true;
timer = null;
}, wait);
}
}
return function (){
if(!timer){
timer = setTimeout(()=>{
func.apply(this, arguments)
timer = null
}, delay)
}
}
}
// 节流+防抖综合
function debounce_throttle(func, delay){
let last = 0;
let timer = null;
return function(){
let context = this;
let args = arguments;
let now = +new Date()
if(now - last < delay){
clearTimeout(timer)
timer = setTimeout(()=>{
last = now;
func.apply(context, args)
},delay)
}else{
last = now;
func.apply(context, args)
}
}
}
function flat(arr) {
let res = []
for(let i = 0; i<arr.length;i++){
if(arr[i] instanceof Array){
res = res.concat(flat(arr[i]))
}
else{
res.push(arr[i])
}
}
return res
}
// console.log(flat([1,2,[3,4,[5,6,[7,8,[9,10]]]],11]))
function _instanceof(left, right){
let _proto = left.__proto__
let prototype = right.prototype
if(_proto === null || typeof _proto !== 'object'){
return false
}else{
if(_proto === prototype){
return true
}
return _instanceof(_proto.__proto__,prototype)
}
}
console.log(_instanceof([1,23,4], Object))
function sort(arr){
for(let i = 0;i<arr.length - 1; i++){
let isSort = false
for(let j = 0; j<arr.length - 1 - i; j++){
if(arr[j+1] > arr[j]){
[arr[j+1],arr[j]] = [arr[j],arr[j+1]]
isSort = true
}
}
if(!isSort) break
}
return arr
}
function sort1(arr){
for(let i = 0;i<arr.length-1;i++){
let min = i;
for(let j = i+1;j<arr.length;j++){
if(arr[min] > arr[j]){
min = j
}
}
[arr[min], arr[i]] = [arr[i], arr[min]]
}
return arr
}
function sort2(arr){
let left = [], right = [],
middle = arr.splice(Math.floor(arr.length/2), 1);
for(let i = 0; i<arr.length;i++){
if(arr[i]>middle){
right.push(arr[i])
}else{
left.push(arr[i])
}
}
return sort(left).concat([middle],sort(right))
}