<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>面向切面</title> <style type="text/css"> </style> </head> <body> <input onclick="voice()" type="button" id="bn" value="动我就叫人来"> <script type="text/javascript"> function voice() { alert("救命啊!"); } Aspects = function() {}; Aspects.prototype = { before: function(target, method, advice) { var original = target[method]; target[method] = function() { (advice)(); original.apply(target, arguments); } return target }, after: function(target, method, advice) { var original = target[method]; target[method] = function() { original.apply(target, arguments); (advice)(); } return target }, around: function(target, method, advice) { var original = target[method]; target[method] = function() { (advice)(); original.apply(target, arguments); (advice)(); } return target } } window.onload = function() { var bn = document.getElementById("bn"); var a = new Aspects; a.after(bn, "onclick", function() { alert("HELP!HELP!") }); } </script> </body> </html>