I'm new to Javascript. Usually write Python/PHP. Currently reading through JavaScript: The Definitive Guide: Activate Your Web Pages (Definitive Guides).
Part of the example code used I'm trying to figure out why it won't work, and the detailed explanation why too if possible, is below over properties getter/setters and inheritance.
The function I'm using:
function inherit(p) {
if (p == null) throw TypeError();
if (Object.create) return Object.create(p);
var t = typeof p;
if (t !== "object" && t !== "function") throw TypeError();
function f() {};
f.prototype = p;
return new f();
}
var p = {
x: 1.0,
y: 1.0,
get r() {
return Math.sqrt(this.x*this.x + this.y*this.y);
},
set r(newvalue) {
var oldvalue = Math.sqrt(this.x*this.x + this.y*this.y);
var ratio = newvalue/oldvalue;
this.x *= ratio;
this.y *= ratio;
},
get theta() { return Math.atan2(this.y, this.x); }
};
When I run a test. I can't see anything being inherited.
> var q = inherit(p)
undefined
> q
{}
> p
{ x: 1, y: 1, r: [Getter/Setter], theta: [Getter] }
Why is this? I am currently using node v6.11.3
on Mac to run this.
Update:
Discovered how to show all values in an Object. Setting enumerable:True
to an object will auto-display all values. What I was trying to figure out how to do. For any who also use Node.js
.