Voyz's Studio.

JavaScript各种源码的手写实现

字数统计: 593阅读时长: 3 min
2021/05/26 Share

JavaScript各种源码的手写实现

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
// ** new **
function myNew(Fuc,args) {
window.obj = {};
Object.setPrototypeOf(window.obj,Fuc.prototype);
const res = Fuc.call(window.obj,args);
return res instanceof Object ? res : window.obj;
}

function Voyz() {
this.name = 'voyz';
this.gender = 'male';
}

// ** Array.isArray **
Array.myIsArray = function (o) {
return o instanceof Array;
}

// ** Object.create **
Object.myCreate = (obj)=>{
const F = function () {};
F.prototype = obj;
return new F();
}

// ** Array.prototype.reduce **
Array.prototype.myReduce = function (callback,init) {
let res = init ? init : this[0];
for(let i=(init ? 0 : 1);i<this.length;i++){
let _this = this;
res = callback(res,this[i],i,_this);
}
return res;
}

// ** call & apply **
Function.prototype.myCall = function(obj) {
const context = obj || window;
context.fn = this;
const args = [...arguments].slice(1);
const res = context.fn(...args);
delete context.fn;
return res;
}

Function.prototype.myApply = function (context) {
context = context || window;
context.fn = this;
let res;
if(arguments[1]){
res = context.fn(...arguments[1])
}else{
res = context.fn();
}
delete context.fn;
return res;
}

// ** Curry **
function createCurry(func,props) {
let args = props || [],
target_len = func.length,
_this = this;

return function () {
let _args = args.concat([...arguments]);
if(_args.length<target_len){
return createCurry.call(_this,func,_args);
}
return func.apply(_this,_args);
}
}

function add(a,b,c,d) {
return a+b+c+d
}

// ** debounce/throttle **
const debounce = (func,wait) => {
let timer;
return function () {
let context = this,
args = [...arguments];
if(timer) clearTimeout(timer);
timer = setTimeout(() => {
func.apply(context,args)
timer = null
}, wait);
}
}

const throttle = (func,delay) => {
let timer;
return function () {
let context = this,
args = arguments;
if(timer) return;
timer = setTimeout(() => {
func.apply(context,args)
timer = null
}, delay);
}
}

const _onchange = (params)=>{
if(params.nativeEvent.data){
console.log(params.nativeEvent.data);
}
}

// ** 深拷贝 **
function deepClone(target) {
if(typeof target === 'object'){
let res = Array.isArray(target) ? [] : {};
for(const key in target){
res[key] = deepClone(target[key])
}
return res;
}else{
return target
}
}

// ** instanceof **
function myInstanceof(L,R) {
const target = R.prototype;
while(true){
if(L === null) return false;
if(L === target) return true;
L = L.__proto__;
}
}

// ** 原型链继承 **
function myExtends(C,P) {
const F = function () {};
F.prototype = P.prototype;
C.prototype = new F();
C.prototype.constructor = C;
C.super = P.prototype;
}

** flat **
Array.prototype.myFlat = function(isFirstLevel=true) {
if(isFirstLevel && !Array.isArray(this)){
throw `${this}.flat is not a function`;
}else{
let res = []
this.forEach(item=>{
if(Array.isArray(item)){
res = res.concat(item.myFlat(false))
}else{
res.push(item)
}
})
return res;
}
}
Array.prototype.theFlat = function (number=1) {
if(number>0 && !this instanceof Array){
throw `${this}.flat is not a function`;
}else{
let res = [];
this.forEach(item=>{
if(item instanceof Array && number === 0){
res = res.concat(item.theFlat(--number))
}else{
res.push(item)
}
})
return res;
}
}

// ** map **
Array.prototype.myMap = function(callback,_this) {
const res = [];
for(let i=0;i<this.length;i++){
res.push(callback.call(_this,this[i],i,this))
}
return res;
};
CATALOG
  1. 1. JavaScript各种源码的手写实现