> --
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en.
>
--
Linus G Thiel
Hansson & Larsson
http://hanssonlarsson.se/
+46 709 89 03 85
"Classes" in JavaScript are just functions with a "prototype" property
that's been dressed up. The "call" method on a Function object
executes the function in the this-context of the supplied object.
fn.call(obj, 1, 2, 3)
// is kinda like
obj.fn(1, 2, 3)
So, doing `ParentClass.call(this, x, y, z)` (or the more general:
ParentClass.apply(this, arguments)) is like saying "Apply the parent
class constructor to this object, as if we'd done `new ParentClass(x,
y, z)`, but do it to my object instead of creating a new one."
function Point2D (x, y) {
this.x = x
thix.y = y
}
Point2D.prototype =
{ distance: function (p) {
return Math.pow(Math.pow(p.x - this.x, 2) + Math.pow(p.y - this.y, 2), 0.5)
}
}
function Point3D (x, y, z) {
// call the parent ctor
Point2D.call(this, x, y)
this.z = z
}
// extend the prototype of the parent
Point3D.prototype = Object.create(Point2D.prototype, { constructor: {
value: Point3D }})
// override the distance method to make it 3d
Point3D.prototype.distance = function (p) {
return Math.pow(Math.pow(p.x - this.x, 2) + Math.pow(p.y - this.y,
2) + Math.pow(p.z - this.z, 2), 0.5)
}
> --
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/nodejs/-/udKTIwlQ0i0J.
Even though it might not do anything today, if any functionality ever
*is* added to the EventEmitter ctor, you probably want it. So, it's a
good practice regardless.