hitode909の日記

以前はプログラミング日記でしたが、今は子育て日記です

こういうのあったら汎用的に使えて良いかと思ったけどそうでもないかもしれない.既存のライブラリなどを上書きするときに使えると思う.

var around = function(fun, body) {
    return function() {
        body(fun);
    };
};

var before = function(fun, body) {
    return function() {
        body();
        fun();
    };
};

var after = function(fun, body) {
    return function() {
        fun();
        body();
    };
};

var hello = function() {
    print('hello');
};


hello = before(hello, function() {
    print('before');
});

hello = after(hello, function() {
    print('after');
});

hello = around(hello, function(f) {
    print('around(1)');
    f();
    print('arount(2)');
});

hello();
around(1)
before
hello
after
arount(2)


引数がちゃんと渡ってくるようにするとよさそうかと思ったけどそうでもないかもしれない.