Decorators in Javascript

I've found plenty of examples of the Class decorator around the internets, but none of the function decorator I've come to know and love in python, so here it is:


x = function(msg) {
alert(msg);
return msg;
}
my_decorator = function(fn) {
var that = this;
return function() {
msg = "Message: " + msg;
return fn.apply(that, arguments);
}
}
x = my_decorator(x);
x('qwery');

// result: an alert that says, "Message: querty"

 
Please Upgrade to Firefox

Um... it looks like you're using Internet Explorer. No, no... there's nothing wrong with that, it's just that... well... it kind of sucks.

You don't have to upgrade for this site, but IE has a lot of problems. Please Upgrade to Firefox (don't worry it's free).

Subscribe