Skip to content

Instantly share code, notes, and snippets.

@Teino1978-Corp
Forked from dnasca/jsGettersAndSetters.js
Created October 30, 2015 18:12
Show Gist options
  • Save Teino1978-Corp/5bb154a8272c9636ae2c to your computer and use it in GitHub Desktop.
Save Teino1978-Corp/5bb154a8272c9636ae2c 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