Created
February 18, 2013 06:14
-
-
Save dgs700/4975399 to your computer and use it in GitHub Desktop.
FICO gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
//make an array of Widgets | |
var widgetArray = [], //array of widgets | |
widget, //widget | |
w, i; //temp vars | |
//widget constructor | |
function Widget(name, gears, angle) { | |
//assign parameters or defaults | |
this.name = name || 'widget'; | |
this.gears = gears || 0; | |
this.angle = angle || 0; | |
} | |
//rotate angle clockwise 90 degrees | |
Widget.prototype.rotate = function () { | |
this.angle = (this.angle + 90) % 360; | |
return this; | |
} | |
//gears accessor | |
Widget.prototype.getGears = function () { | |
return this.gears; | |
} | |
//gears mutator | |
Widget.prototype.setGears = function (gears) { | |
this.gears = gears; | |
return this; | |
} | |
//angle accessor | |
Widget.prototype.getAngle = function () { | |
return this.angle; | |
} | |
//angle mutator | |
Widget.prototype.setAngle = function (angle) { | |
this.angle = angle; | |
return this; | |
} | |
//reset angle to 0 degrees | |
Widget.prototype.resetAngle = function () { | |
this.angle = 0; | |
return this; | |
} | |
//name accessor | |
Widget.prototype.getName = function () { | |
return this.name; | |
} | |
//name mutator | |
Widget.prototype.setName = function (newName) { | |
this.name = newName; | |
return this; | |
} | |
//return a new widget instance with current properties | |
Widget.prototype.clone = function () { | |
return (new Widget(this.name, this.gears, this.angle)); | |
} | |
// widget.toString and log to console | |
Widget.prototype.print = function () { | |
console.log( this.name + " widget has " + this.gears + " gears and a " + this.angle + " degree angle " ); | |
} | |
// add a new widget to the array | |
widgetArray.push( new Widget( "first", 3, 90 )); | |
//copy first widget but change its gears and rotate | |
widget = widgetArray[0].clone().setGears(4).rotate(); | |
//add it to the array | |
widgetArray.push( widget); | |
//let's make just one more | |
widgetArray[widgetArray.length] = widgetArray[0].clone().setName( "third").setGears(6); | |
//print out some info about our widgets | |
for (i = 0, len = widgetArray.length; i < len; i++) { | |
w = widgetArray[i]; | |
w.rotate().print(); | |
//oops, our second gear should have its own name and no angle | |
w.setName("second").setAngle(0).print(); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment