Skip to content

Instantly share code, notes, and snippets.

@sri-rang
Created December 25, 2011 15:53
Show Gist options
  • Save sri-rang/1519451 to your computer and use it in GitHub Desktop.
Save sri-rang/1519451 to your computer and use it in GitHub Desktop.
Functional Programming in JavaScript - 8
const CIRCLE = "circle";
const SQUARE = "square";
const RECTANGLE = "rectangle";
var getAreaFunction = function (shape) {
return function () {
switch (shape) {
case CIRCLE:
return Math.PI * arguments[0] * arguments[0];
break;
case SQUARE:
return arguments[0] * arguments[0];
break;
case RECTANGLE:
return arguments[0] * arguments[1];
break;
}
};
};
var Shape = function (type) {
var area = getAreaFunction(type);
return {
getArea:function () {
return Math.round(Number(area.apply(this, arguments)));
}
};
};
var aCircle = new Shape(CIRCLE);
var aSquare = new Shape(SQUARE);
var aRectangle = new Shape(RECTANGLE);
console.log(aCircle.getArea(50));
console.log(aSquare.getArea(50));
console.log(aRectangle.getArea(50, 20));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment