Skip to content

Commit 9562e9d

Browse files
committed
objects-classes, ch3: more tweaks for clarity
1 parent f394631 commit 9562e9d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

objects-classes/ch3.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ point.setX(3);
196196

197197
The `setX` property (method) *looks like* it exists on (is owned by) the `point` object here. But that's a mirage. Each class method is added to the `prototype`object, a property of the constructor function.
198198

199-
So, `setX(..)` only exists as `Point2d.prototype.greeting`. Since `point` is `[[Prototype]]` linked to `Point2d.prototype` (see Chapter 2) via the `new` keyword instantiation, the `point.setX(..)` reference traverses the `[[Prototype]]` chain and finds the method to execute.
199+
So, `setX(..)` only exists as `Point2d.prototype.setX`. Since `point` is `[[Prototype]]` linked to `Point2d.prototype` (see Chapter 2) via the `new` keyword instantiation, the `point.setX(..)` reference traverses the `[[Prototype]]` chain and finds the method to execute.
200200

201201
Class methods should only be invoked via an instance; `Point2d.setX(..)` doesn't work because there *is no* such property. You *could* invoke `Point2d.prototype.setX(..)`, but that's not generally proper/advised in standard class-oriented coding. Always access class methods via the instances.
202202

@@ -237,7 +237,7 @@ Instead of defining a class instance member imperatively via `this.` in the cons
237237

238238
```js
239239
class Point2d {
240-
// this is a public field
240+
// these are public fields
241241
x = 0
242242
y = 0
243243

@@ -450,7 +450,7 @@ var point = new Point3d();
450450
point.printX(); // double x: 42
451451
```
452452

453-
The `Point3d` subclass overrides the inherited `getX()` method to give it different behavior. However, you can still instantiate the base `Point2d` class, which would then give an object that uses the original (`"That's ..."`) definition for `greeting()`.
453+
The `Point3d` subclass overrides the inherited `getX()` method to give it different behavior. However, you can still instantiate the base `Point2d` class, which would then give an object that uses the original (`return this.x;`) definition for `getX()`.
454454

455455
If you want to access an inherited method from a subclass even if it's been overriden, you can use `super` instead of `this`:
456456

@@ -482,7 +482,7 @@ var point = new Point3d();
482482
point.printX(); // x: 21
483483
```
484484

485-
The ability for methods of the same name, at different levels of the inheritance hierarchy, to exhibit different behavior when either accessed directly, or relatively with `super`, is called *polymorphism*. It's a very powerful part of class-orientation, when used appropriately.
485+
The ability for methods of the same name, at different levels of the inheritance hierarchy, to exhibit different behavior when either accessed directly, or relatively with `super`, is called *method polymorphism*. It's a very powerful part of class-orientation, when used appropriately.
486486

487487
### That's Super!
488488

@@ -555,6 +555,8 @@ var point = new Point3d(3,4,5);
555555
// Setting instance property 'z' to 5
556556
```
557557

558+
As the console messages illustrate, the `z = ..` field initialization happens *immediately after* the `super(x,y)` call, *before* the ``console.log(`Setting instance...`)`` is executed. Perhaps think of it like the field initializations attached to the end of the `super(..)` call, so they run before anything else in the constructor does.
559+
558560
#### Which Class?
559561

560562
You may need to determine in a constructor if that class is being instantiated directly, or being instantiated from a subclass with a `super()` call. We can use a special "pseudo property" `new.target`:
@@ -700,7 +702,7 @@ Where is that `toString()` method located? On the prototype object:
700702
Object.hasOwn(Point3d.prototype,"toString"); // true
701703
```
702704

703-
And `b` has access to that method via its `[[Prototype]]` linkage (see Chapter 2). In other words, the prototype objects **share access** to their method(s) with the subclass(es) and instance(s). The method(s) stay in place, and are not copied down the inheritance chain.
705+
And `anotherPoint` has access to that method via its `[[Prototype]]` linkage (see Chapter 2). In other words, the prototype objects **share access** to their method(s) with the subclass(es) and instance(s). The method(s) stay in place, and are not copied down the inheritance chain.
704706

705707
As nice as the `class` syntax is, don't forget what's really happening under the syntax: JS is *just* wiring up objects to each other along a `[[Prototype]]` chain.
706708

0 commit comments

Comments
 (0)