Open lgwebdream opened 4 years ago
~(function(){
Function.prototype.before = function(beforeFn){
return (...args) => {
// 先执行传入的beforeFn的函数
beforeFn.apply(this, args);
// 再执行调用beforeFn的函数
const res = this.apply(this, args);
return res;
}
}
Function.prototype.after = function(afterFn){
return (...args) => {
// 先执行调用的after的函数
const res = this.apply(this, args);
// 再执行传入的afterFn函数
afterFn.apply(this, args);
return res;
}
}
function A() {
console.log("调用了函数A");
}
const fn = A.before(() => {
console.log('before');
}).after(() => {
console.log('after');
});
fn();
})();
```js
~(function(){
Function.prototype.before = function(beforeFn){
return (...args) => {
// 先执行传入的beforeFn的函数
beforeFn.apply(this, args);
// 再执行调用beforeFn的函数
const res = this.apply(this, args);
return res;
}
}
Function.prototype.after = function(afterFn){
return (...args) => {
// 先执行调用的after的函数
const res = this.apply(this, args);
// 再执行传入的afterFn函数
afterFn.apply(this, args);
return res;
}
}
function A() {
console.log("调用了函数A");
}
const fn = A.before(() => {
console.log('before');
}).after(() => {
console.log('after');
});
fn();
})();
function A() {
console.log("调用了函数A");
}
const B = A;
A = () => {
console.log('HelloWorld');
B()
}
A()
function wrapWidthA(A) {
return function() {
console.log('HelloWorld');
return A.apply(this, arguments);
}
}
function A() {
console.log("调用了函数A");
}
const A = wrapWidthA(A);