Created
November 7, 2014 03:23
-
-
Save hacke2/720c70ec10b090d035ef to your computer and use it in GitHub Desktop.
js面向切面
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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