函数式编程之AOP面向切面编程
文章目录
假设我们目前有一个小需求:
实现目标函数调用前后的业务逻辑方法。
// intro 实现test函数调用前后的业务逻辑方法;
function test() {
console.log(2);
};
// 调用函数
test();
// expect 期望输出如下
// console.log(1);
// console.log(2);
// console.log(3);思考一下我们将会如何实现?
🤔🤔
难点:
1、方法在绑在哪?
2、函数的执行顺序。
3、this指向如何保证?
4、参数如何保证?
思路:
事实上我们知道在js代码中任何函数都是Function构造函数的实例对象,
既然要实现额外的功能就一定会重写函数本身,意味着全新的函数,
故可以将方法绑定在所有函数的原型上;
实现如下:
// intro 实现test函数调用前后的业务逻辑方法;
function test() {
console.log(2);
};
// 绑定方法
Function.prototype.before = function(beforeFn) {
const _this = this; // test调用 指向 原函数
return function() { // return 中间函数
beforeFn(); // 前置函数
_this(); // 原函数 test
};
};
Function.prototype.after = function(afterFn) {
const _this = this; // 中间函数
return function() { // 最终重写后的 test函数
_this(); // 中间函数
afterFn(); // 后置函数
};
};
// 重写
test = test.before(function() {
console.log(1);
})
.after(function() {
console.log(3);
})
// 调用函数
test();
// 输出如下
// console.log(1);
// console.log(2);
// console.log(3);如此就能实现函数调用前后的业务逻辑方法啦。
不完美:
同样也是经典的this指向问题,函数的自调用,执行的都是预编译的流程,this都将指向window,看看如下代码
function test() {
console.log(this); // window
};
Function.prototype.before = function(beforeFn) {
const _this = this;
return function() {
beforeFn();
_this();
};
};
Function.prototype.after = function(afterFn) {
const _this = this;
return function() {
_this();
afterFn();
};
};
test = test.before(function() {
console.log(this); // window
})
.after(function() {
console.log(this); // window
})
// 调用函数
test();
遗漏的问题? this的指向如何保证呢?
function test() {
console.log(this); // obj
};
Function.prototype.before = function(beforeFn) {
const _this = this;
return function() {
console.log(this) // this => Tag的this => obj
beforeFn.call(this);
_this.call(this);
};
};
Function.prototype.after = function(afterFn) {
const _this = this;
return function() { // Tag
console.log(this); // 这里的this => test.call(obj) => obj
_this.call(this); // 再次调用call方法将_this函数的this拿到 => obj
afterFn.call(this);
};
};
-----------------
// 利用对象
const obj = {
test: test;
}
------------------
// 注意左侧test的实际函数为Tag处标记的函数
test = obj.test.before(function() {
console.log(this); // obj
})
.after(function() {
console.log(this); // obj
})
// 虽然利用了对象但是this依旧没有改变,我们采用call方法调用改变this指向
test.call(obj);
依旧不完美:
参数的传递如何保证?
function test(a, b, c) {
console.log([a, b, c]); // ['one', 'two', 'three']
};
Function.prototype.before = function(beforeFn) {
const _this = this;
return function() {
beforeFn.apply(this, arguments);
_this.apply(this, arguments);
};
};
Function.prototype.after = function(afterFn) {
const _this = this;
return function() { // Tag
console.log(arguments); // arguments实参列表,类数组与形参相互映射
_this.apply(this, arguments);
afterFn.apply(this, arguments);
};
};
const obj = {
test: test;
}
test = obj.test.before(function(a, b, c) {
console.log([a, b, c]); // ['one', 'two', 'three']
})
.after(function(a, b, c) {
console.log([a, b, c]); // ['one', 'two', 'three']
})
test.call(obj, 'one', 'two', 'three');
// 输出
// ['one', 'two', 'three']
// ['one', 'two', 'three']
// ['one', 'two', 'three']
终于我们完整的实现了这个小需求~~
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论