Created
October 20, 2015 07:57
-
-
Save dnasca/3fbcb45584adb94eb127 to your computer and use it in GitHub Desktop.
Getters and Setters in JS, the easy way.
This file contains hidden or 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
//getters and settings done easy | |
var Circle = function(radius) { | |
this._radius = radius; | |
}; | |
Circle.prototype = { | |
set radius(radius) {this._radius = radius;}, | |
get radius() { return this._radius;}, | |
get area() { return Math.PI * (this._radius * this._radius);} | |
}; | |
var circ = new Circle(10); | |
//set prop | |
circ.radius = 15; | |
console.log("A circle with a radius of " + circ.radius + " has an area of " + circ.area.toFixed(2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment