O'Reilly logo
  • Sumit Kaushik thinks this is interesting:

Accessor properties are inherited, just as data properties are, so you can use the object p defined above as a prototype for other points. You can give the new objects their own x and y properties, and they’ll inherit the r and theta properties:

var q = inherit(p); ...


Cover of JavaScript: The Definitive Guide, 6th Edition


var p = { // x and y are regular read-write data properties. x: 1.0, y: 1.0,

// r is a read-write accessor property with getter and setter. // Don't forget to put a comma after accessor methods. get r() { return Math.sqrt(this.xthis.x + this.ythis.y); }, set r(newvalue) { var oldvalue = Math.sqrt(this.xthis.x + this.ythis.y); var ratio = newvalue/oldvalue; this.x = ratio; this.y = ratio; },

// theta is a read-only accessor property with getter only. get theta() { return Math.atan2(this.y, this.x); } };

var q = Object.create(p); // Create a new object that inherits getters and setters q.x = 1; q.y = 1; // Create q's own data properti...