Skip to content

Commit c462385

Browse files
committed
🎨 update: cloneFunction
1 parent a4e68e4 commit c462385

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

JavaScript/浅拷贝和深拷贝.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const stringTag = '[object String]';
4040
const symbolTag = '[object Symbol]';
4141
const errorTag = '[object Error]';
4242
const regexpTag = '[object RegExp]';
43+
const funcTag = '[object Function]';
4344

4445
const deepTag = [mapTag, setTag, arrayTag, objectTag, argsTag];
4546

@@ -78,6 +79,28 @@ function cloneReg(targe) {
7879
return result;
7980
}
8081

82+
function cloneFunction(func) {
83+
const bodyReg = /(?<={)(.|\n)+(?=})/m;
84+
const paramReg = /(?<=\().+(?=\)\s+{)/;
85+
const funcString = func.toString();
86+
if (func.prototype) {
87+
const param = paramReg.exec(funcString);
88+
const body = bodyReg.exec(funcString);
89+
if (body) {
90+
if (param) {
91+
const paramArr = param[0].split(',');
92+
return new Function(...paramArr, body[0]);
93+
} else {
94+
return new Function(body[0]);
95+
}
96+
} else {
97+
return null;
98+
}
99+
} else {
100+
return eval(funcString);
101+
}
102+
}
103+
81104
function cloneOtherType(targe, type) {
82105
const Ctor = targe.constructor;
83106
switch (type) {
@@ -91,6 +114,8 @@ function cloneOtherType(targe, type) {
91114
return cloneReg(targe);
92115
case symbolTag:
93116
return cloneSymbol(targe);
117+
case funcTag:
118+
return cloneFunction(targe);
94119
default:
95120
return null;
96121
}
@@ -145,9 +170,4 @@ function clone(target, map = new WeakMap()) {
145170

146171
return cloneTarget;
147172
}
148-
149-
module.exports = {
150-
clone
151-
};
152-
153173
```

docs/JavaScript/浅拷贝和深拷贝.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const stringTag = '[object String]';
4040
const symbolTag = '[object Symbol]';
4141
const errorTag = '[object Error]';
4242
const regexpTag = '[object RegExp]';
43+
const funcTag = '[object Function]';
4344

4445
const deepTag = [mapTag, setTag, arrayTag, objectTag, argsTag];
4546

@@ -78,6 +79,28 @@ function cloneReg(targe) {
7879
return result;
7980
}
8081

82+
function cloneFunction(func) {
83+
const bodyReg = /(?<={)(.|\n)+(?=})/m;
84+
const paramReg = /(?<=\().+(?=\)\s+{)/;
85+
const funcString = func.toString();
86+
if (func.prototype) {
87+
const param = paramReg.exec(funcString);
88+
const body = bodyReg.exec(funcString);
89+
if (body) {
90+
if (param) {
91+
const paramArr = param[0].split(',');
92+
return new Function(...paramArr, body[0]);
93+
} else {
94+
return new Function(body[0]);
95+
}
96+
} else {
97+
return null;
98+
}
99+
} else {
100+
return eval(funcString);
101+
}
102+
}
103+
81104
function cloneOtherType(targe, type) {
82105
const Ctor = targe.constructor;
83106
switch (type) {
@@ -91,6 +114,8 @@ function cloneOtherType(targe, type) {
91114
return cloneReg(targe);
92115
case symbolTag:
93116
return cloneSymbol(targe);
117+
case funcTag:
118+
return cloneFunction(targe);
94119
default:
95120
return null;
96121
}
@@ -145,9 +170,4 @@ function clone(target, map = new WeakMap()) {
145170

146171
return cloneTarget;
147172
}
148-
149-
module.exports = {
150-
clone
151-
};
152-
153173
```

0 commit comments

Comments
 (0)