-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
executable file
·59 lines (52 loc) · 1.66 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>AOP</title>
</head>
<body>
<script>
Function.prototype.before = function (func) {
const _self = this;
return function () {
if (func.apply(this, arguments) === false) {
return false;
}
return _self.apply(this, arguments);
};
};
Function.prototype.after = function (func) {
const _self = this;
return function () {
if (_self.apply(this, arguments) === false) {
return false;
}
return func.apply(this, arguments);
};
};
function exampleFunction() {
console.log("执行主要逻辑");
return "主要逻辑结果";
}
// 在示例函数执行前添加逻辑
const functionWithBefore = exampleFunction.before(function () {
console.log("执行前置逻辑");
// 返回 false 可以中止主要逻辑的执行
// return false;
});
// 在示例函数执行后添加逻辑
const functionWithAfter = exampleFunction.after(function () {
console.log("执行后置逻辑");
});
console.log("调用原始函数:");
const result1 = exampleFunction();
console.log("\n----------------\n");
console.log("调用添加前置逻辑的函数:");
const result2 = functionWithBefore();
console.log("\n----------------\n");
console.log("调用添加后置逻辑的函数:");
const result3 = functionWithAfter();
</script>
</body>
</html>