Skip to content

Instantly share code, notes, and snippets.

@jitensachdeva
Created July 23, 2014 09:59
Show Gist options
  • Save jitensachdeva/a5ea8254f7f67e259af9 to your computer and use it in GitHub Desktop.
Save jitensachdeva/a5ea8254f7f67e259af9 to your computer and use it in GitHub Desktop.

Revisions

  1. jitensachdeva created this gist Jul 23, 2014.
    56 changes: 56 additions & 0 deletions decorator.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    function getDateTime() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth()+1;
    var day = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    if(month.toString().length == 1) {
    var month = '0'+month;
    }
    if(day.toString().length == 1) {
    var day = '0'+day;
    }
    if(hour.toString().length == 1) {
    var hour = '0'+hour;
    }
    if(minute.toString().length == 1) {
    var minute = '0'+minute;
    }
    if(second.toString().length == 1) {
    var second = '0'+second;
    }
    var dateTime = year+'/'+month+'/'+day+' '+hour+':'+minute+':'+second;
    return dateTime;
    }



    var add = function(a,b) {
    console.log(a+b);
    return a+b;
    };

    var subtract = function(a,b){
    console.log(a-b);
    return a-b
    }


    var decorator = function (block){
    return function(){
    console.log("start time - " + getDateTime() );
    block.apply(this,arguments);
    console.log("end time -" + getDateTime());
    }
    };

    var addDecorated = decorator(add);

    addDecorated(1,2);

    var subtractDecorated = decorator(subtract);
    subtractDecorated(3,4)