LoginSignup
5
6

More than 5 years have passed since last update.

アスペクト指向(AOP)

Last updated at Posted at 2012-06-15
    //for AOP
    function _after(target, methodName, aspect) {

        var method = target[methodName];

        //overwrite the method.
        target[methodName] = function () {

            var args = Array.prototype.slice.call(arguments),
                  result;

            result = method.apply(this, arguments);
            args.push(result);
            args.push({
                target: this,
                methodName: methodName,
                method: method
            });

            return aspect.apply(this, args);
        };
    }

    function _apply(func, target, methodNames, aspect) {

        if (Object.prototype.toString.call(methodNames) !== '[object Array]') {
            methodNames = [methodNames];
        }

        var i = 0, mName;

        for (; mName = methodNames[i]; i++) {
            func(target, mName, aspect);
        }
    }

    function after(target, methodNames, aspect) {

        _apply(_after, target, methodNames, aspect);
    }
5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6