Skip to content

Instantly share code, notes, and snippets.

@ashgti
Created October 1, 2010 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashgti/606320 to your computer and use it in GitHub Desktop.
Save ashgti/606320 to your computer and use it in GitHub Desktop.
protoclass A {
has $.x;
submethod BUILD {
$.x = 1;
}
}
A.^prototype('foo', method {
return 'A::foo';
});
protoclass B is A.new {
submethod BUILD {
$.x = 2;
}
}
B.^prototype('foo', method {
say self.A.foo;
return 'B::foo';
});
B.^prototype('bar', method {
return 'B::bar';
});
protoclass C is A.new {
submethod BUILD {
$.x = 3;
}
}
C.^prototype('baz', method {
return 'C::baz';
});
protoclass D is B.new is C.new {
submethod BUILD {
$.x = 4;
}
}
my $d = D.new;
say $d.baz; #=> "C.baz"
say $d.bar; #=> "A.foo\nB.bar"
say $d.foo; #=> "B.foo"
function A() {
this.x = 1;
}
A.prototype.foo = function () {
return 'A.foo';
}
B.prototype = new A;
B.prototype.constructor = B;
function B() { // New special constructor for B
this.x = 3;
}
B.prototype.foo = function() { // overrides A.foo
return "B.foo";
}
B.prototype.bar = function() {
console.log(A.prototype.foo.call(this)); // super like function call
return 'B.bar';
}
C.prototype = new A;
C.prototype.constructor = C;
function C() {
this.x = 4;
}
C.prototype.baz = function() {
return 'C.baz';
}
var mult = new B;
mult = merge(new C, mult);
D.prototype = mult; // multi-inheritence
D.prototype.constructor = D;
function D() {
this.x = 5;
}
var d = new D;
console.log(d.baz());
console.log(d.bar());
console.log(d.foo());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment