Skip to content

Instantly share code, notes, and snippets.

@dnasca
Created October 20, 2015 07:57
Show Gist options
  • Save dnasca/3fbcb45584adb94eb127 to your computer and use it in GitHub Desktop.
Save dnasca/3fbcb45584adb94eb127 to your computer and use it in GitHub Desktop.
Getters and Setters in JS, the easy way.
//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