Skip to content

Instantly share code, notes, and snippets.

@DimitarChristoff
Created October 29, 2013 15:11
Show Gist options
  • Save DimitarChristoff/7216505 to your computer and use it in GitHub Desktop.
Save DimitarChristoff/7216505 to your computer and use it in GitHub Desktop.
require(['prime/prime'], function(prime){
var Rectangle = prime({
constructor: function(width, height){
return this.setwidth(width).setHeight(height);
},
setWidth: function(width){
this.width = width;
return this; // allow chaining
},
setHeight: function(height){
this.height = height;
return this;
},
squareRoot: function(){
return this.height * this.width;
}
});
var Square = prime({
// subclass of Rectangle
extend: Rectangle,
constructor: function(side){
return this.setSide(side);
},
setSide: function(side){
// both sides are the same
this.width = this.height = side;
return this;
},
setWidth: function(width){
return this.setSide(width);
},
setHeight: function(height){
return this.setSide(height);
}
});
var square = new Square(30);
square.setWidth(5); // local
console.log(square.height); // 5
console.log(square.squareRoot()); // from parent proto of Rectangle, 25
console.log(square instanceof Rectangle); // true
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment