Skip to content

Instantly share code, notes, and snippets.

@hacke2
Created November 7, 2014 03:23
Show Gist options
  • Select an option

  • Save hacke2/720c70ec10b090d035ef to your computer and use it in GitHub Desktop.

Select an option

Save hacke2/720c70ec10b090d035ef to your computer and use it in GitHub Desktop.

Revisions

  1. hacke2 created this gist Nov 7, 2014.
    71 changes: 71 additions & 0 deletions AOP.JS
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    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()*/