Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Created July 6, 2011 22:47
Show Gist options
  • Save francescoagati/1068527 to your computer and use it in GitHub Desktop.
Save francescoagati/1068527 to your computer and use it in GitHub Desktop.
wrapping a method of a prototype Object in javascript e coffeescript
class A
say_something: -> alert "hello"
class B extends A
#create new instance of B and call say_something
new B().say_something()
#wrapping method say_something of prototype A
A::say_something= do ->
fn=A::say_something
return ->
alert "before say"
fn.apply @
alert "after say"
#create new instance of B and call say_something wrapped
new B().say_something()
var A, B;
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
A = (function() {
function A() {}
A.prototype.say_something = function() {
return alert("hello");
};
return A;
})();
B = (function() {
__extends(B, A);
function B() {
B.__super__.constructor.apply(this, arguments);
}
return B;
})();
new B().say_something();
A.prototype.say_something = (function() {
var fn;
fn = A.prototype.say_something;
return function() {
alert("before say");
fn.apply(this);
return alert("after say");
};
})();
new B().say_something();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment