Skip to content

Instantly share code, notes, and snippets.

@rhyolight
Created March 12, 2010 16:39
Show Gist options
  • Save rhyolight/330495 to your computer and use it in GitHub Desktop.
Save rhyolight/330495 to your computer and use it in GitHub Desktop.
// Method 2: constructor returning an object literal with methods attached
function Rectangle2(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 }
};
}
function PositionedRectangle2(w,h,xIn,yIn) {
var x = xIn,
y = yIn,
rect = new Rectangle2(w,h),
me = {
getX: function() { return x; },
setX: function(xIn) { this.x = xIn; },
getY: function() { return y; },
setY: function(yIn) { this.y = yIn; }
};
return mixin(rect, me);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment