Aop example

Hi,
Sorry to bother you
I found this snippet


function attachAfterReturning(obj,name,advice) {
    var old = obj[name];
    obj[name] = function(){
      var result = old.apply(this,arguments);
      advice.call(this,result);
      return result;
  }
}


at http://www.slideshare.net/elazutkin/exciting-javascript-part-i
slide 51

Could you show me how to use in a enlightening example ?

:slight_smile:

Bye.

That slide occurs at 58:38 of the Exciting JavaScript Part 1 video. The AOP section ([url=“http://en.wikipedia.org/wiki/Aspect-oriented_programming”]Aspect-Oriented Programming) starts at 53:04 of the video

It’s where you can request that something is done at different stages of a method being called.

What the above function does, is to replace the method obj[name] so that the return value is returned not just once, but twice. The first place the first is returned is to the advice method, and the second place the result is returned is to whatever calls obj[name]

For example, you have a duck that when asked to speak, it goes “quack”.


var duck = {
    'speak': function () {
        return 'Quack!';
    }
};
alert(duck.speak());
// An alert appears saying 'Quack!'

What it says can also appear in a log, by using the attachAfterReturning function.


function logDuck(message) {
    console.log('The duck says ' + message);
}
attachAfterReturning(duck, 'speak', logDuck);

Now when you call duck.speak(), it will also be sent to logDuck


alert(duck.speak());
// The console.log says 'The duck says Quack!' and
// an alert appears saying 'Quack!'

In your example, an array is returned from the speak function, but javascript when used with a string, such as in the logDuck function, javascript converts the array to a comma-separated string for you.

I suspect that the speak function in this example should more correctly return just a string, instead of an array.

Thanks. For some reason I now feel that I should start singing “A spoonful of sugar helps the medicine go down.”

You’re right there, but the slideshow uses advice.call instead of advice.apply

With the apply method, all of the arguments are given in the array. With the call method, all of the arguments are given as separate function parameters.

I’ve updated the code sections above so that they match the slideshow, which means that this is now no longer an issue.

Thanks both for the reply and for the hint .
I managed this


function attachAfterReturning(obj,name,advice) {
    var old = obj[name];
    obj[name] = function(){
      var result = old.apply(this,arguments);
      advice.call(this,result);
      return result;
    }
}
var duck = {
    'speak': function () {
        return ['Quack!'];
    }
};
alert(duck.speak());
function logDuck(message) {
    alert('The duck says ' + message);
}
attachAfterReturning(duck, 'speak', logDuck);
alert(duck.speak());

only speak is passed as a string isn’t it ?

PS.
very funny :slight_smile:

sorry to bother you again but if speak return a string


var duck = {
    'speak': function () {
        return 'Quack!';
    }
};


I’ve got in Firebug console
second argument to Function.prototype.apply must be an array

PS
Very good link I missed it