-
Notifications
You must be signed in to change notification settings - Fork 896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
有这样一个函数 A,要求在不改变原有函数 A 功能以及调用方式的情况下,使得每次调用该函数都能在控制台打印出“HelloWorld” #800
Labels
Comments
~(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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: