Skip to content

Instantly share code, notes, and snippets.

View rhyolight's full-sized avatar

Matthew Taylor rhyolight

View GitHub Profile
Rectangle = function(w,h) {
return {
width: w,
height: h,
area: function() { return w * h },
};
};
var rect = new Rectangle(2,3);
document.write('area: ' + rect.area() + '<br/>');
var rect = new Rectangle(2,3);
document.write('area : ' + rect.area() + '<br/>');
rect.width = 5;
document.write('area : ' + rect.area() + '<br/>');
area: function() { return w * h }
area: function() { return this.width * this.height }
Rectangle = function(w,h) {
return {
width: w,
height: h,
area: function() { return this.width * this.height },
toString: function() { return '(' + this.width + 'X' + this.height + ')' }
};
};
var rect = new Rectangle(2,3);
document.write('area of ' + rect + ': ' + rect.area() + '<br/>');
rect.width = 5;
document.write('area of ' + rect + ': ' + rect.area() + '<br/>');
Rectangle = function(w,h) {
var width = w, height = h;
return {
getWidth: function() { return width; },
getHeight: function() { return height; },
setWidth: function(w) { width = w; },
setHeight: function(h) { height = h; },
area: function() { return width * height },
toString: function() { return '(' + width + 'X' + height + ')' }
};
var rect = new Rectangle(2,3);
document.write('area of ' + rect + ': ' + rect.area() + '<br/>');
rect.setWidth(5);
document.write('area of ' + rect + ': ' + rect.area() + '<br/>');
Rectangle.prototype.perimeter = function() {
return 2 * this.getWidth() + 2 * this.getHeight();
};