Skip to content

Instantly share code, notes, and snippets.

@emonkak
Created December 11, 2011 16:11
Show Gist options
  • Save emonkak/1461320 to your computer and use it in GitHub Desktop.
Save emonkak/1461320 to your computer and use it in GitHub Desktop.
inherit.js
function Klass(x)
{
this.member = {
x: x,
};
}
Klass.prototype = {
get x() {
return this.member.x;
},
set x(x) {
this.member.x = x;
},
};
function KlassDouble(x)
{
Klass.call(this, x);
}
KlassDouble.prototype = {
__proto__: Klass.prototype,
get y() {
return this.x * 2;
},
set y(y) {
this.x = y * 2;
},
};
var klass = new KlassDouble(1);
console.log(klass.x); // 1
console.log(klass.y); // 2
klass.x = 10;
console.log(klass.x); // 10
console.log(klass.y); // 20
klass.y = 10;
console.log(klass.x); // 20
console.log(klass.y); // 40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment