I need a super-readable version of this super simple inheritance in JavaScript. This is some auto-generated code, where I can’t afford to be using external functions or libraries.
What I really want is, assuming Point3d “inherits” from Point, I want something like this:
function Point(x,y) {
this.x = x;
this.y = y;
}
function Point3d(x,y,z) {
Point(x, y);
this.z = z;
}
Except it doesn’t actually work:
var pt = new Point3d(230,30,11);
// but x and y are in global scope, they should be in pt.x and pt.y :(
One possible option, would in the code generation duplicate all the members — but since Javascript is prototype based, I would imagine this is easy to do properly (if I actually knew Javascript)
Thanks
Apply the
Pointconstructor to thePoint3dobject using.call().http://jsfiddle.net/MSfu5/
The
.callmethod sets the value ofthisin the function you’re calling.Here we’re setting it to the new
Point 3dobject being constructed.If there’s anything on the
Pointprototype thatPoint3dshould inherit, you can make thePoint3d.prototypeobject an empty instance ofPoint.