Skip to content

Instantly share code, notes, and snippets.

@hacke2

hacke2/AOP.JS

Created Nov 7, 2014
Embed
What would you like to do?
js面向切面
Function.prototype.before = function(func) {
var that = this;
return function() {
//debugger
if(func.apply(this, arguments) === false) {
return false;
}
return that.apply(this, arguments);
}
}
Function.prototype.after = function(func) {
var that = this;
return function() {
var ret = that.apply(this, arguments);
if(ret === false) {
return false;
}
func.apply(this, arguments);
return ret;
}
}
function logTime (func, funcName) {
return func = (function() {
var d;
return func.before(function() {
d = +new Date();
}).after(function() {
console.log(+new Date() - d, funcName);
});
})()
}
function logBefore() {
if(window.console) {
window.console.log(1);
}
}
function logAfter() {
if(window.console) {
window.console.log(3);
}
}
function func(msg) {
console.info(msg);
}
function func2(msg) {
var temp = 0
sleep(1000)
return temp;
}
function sleep(maxtime) {
var now = +new Date();
while(true) {
if(+new Date() - now > maxtime) {
break;
}
}
}
/*func = func.before(logBefore).after(logAfter);
func(2) //1,2,3*/
/*func2 = logTime(func2, "fuc")
func2()*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.