@@ -16,7 +16,7 @@ quote}}
16
16
17
17
[ Chapter ?] ( data ) introduced JavaScript's ((object))s. In programming
18
18
culture, we have a thing called _ ((object-oriented programming))_ , a
19
- set of techniques that uses objects (and related concepts) as the
19
+ set of techniques that use objects (and related concepts) as the
20
20
central principle of program organization.
21
21
22
22
Though no one really agrees on its precise definition, object-oriented
@@ -297,7 +297,7 @@ function, and returned at the end of the function.
297
297
298
298
{{index "prototype property"}}
299
299
300
- The appropriate prototype object for a constructor is found by taking
300
+ The prototype object used when constructing objects is found by taking
301
301
the ` prototype ` property of the constructor function.
302
302
303
303
{{index "rabbit example"}}
@@ -336,6 +336,15 @@ is `Function.prototype`, since constructors are functions. Its
336
336
` prototype ` _ property_ holds the prototype used for instances created
337
337
through it.
338
338
339
+ ```
340
+ console.log(Object.getPrototypeOf(Rabbit) ==
341
+ Function.prototype);
342
+ // → true
343
+ console.log(Object.getPrototypeOf(weirdRabbit) ==
344
+ Rabbit.prototype);
345
+ // → true
346
+ ```
347
+
339
348
## Class notation
340
349
341
350
So JavaScript ((class))es are ((constructor)) functions with a
@@ -370,10 +379,11 @@ constructor definition from the previous section. It just looks nicer.
370
379
371
380
{{index [ "class declaration", properties] }}
372
381
373
- Class declarations only allow _ methods_ —properties that hold
382
+ Class declarations currently only allow _ methods_ —properties that hold
374
383
functions—to be added to the ((prototype)). This can be somewhat
375
- inconvenient when you want to save a non-function value in there. You
376
- can still create such properties by directly manipulating the
384
+ inconvenient when you want to save a non-function value in there.
385
+ The next version of the language will probably improve this. For now, you
386
+ can create such properties by directly manipulating the
377
387
prototype after you've defined the class.
378
388
379
389
Like ` function ` , ` class ` can be used both in statement and in
@@ -392,9 +402,9 @@ console.log(object.getWord());
392
402
{{index "shared property", overriding}}
393
403
394
404
When you add a ((property)) to an object, whether it is present in the
395
- prototype or not, the property is added to the object _ itself_ , which
396
- will henceforth have it as its own property. If there _ is _ a property
397
- by the same name in the prototype, this property will no longer affect
405
+ prototype or not, the property is added to the object _ itself_ .
406
+ If there was already a property with
407
+ the same name in the prototype, this property will no longer affect
398
408
the object, as it is now hidden behind the object's own property.
399
409
400
410
```
@@ -524,6 +534,8 @@ console.log(`Júlia is ${ages.get("Júlia")}`);
524
534
// → Júlia is 62
525
535
console.log("Is Jack's age known?", ages.has("Jack"));
526
536
// → Is Jack's age known? false
537
+ console.log(ages.has("toString"));
538
+ // → false
527
539
```
528
540
529
541
{{index interface, "set method", "get method", "has method", encapsulation}}
@@ -669,8 +681,8 @@ When called, that method should return an object that provides a
669
681
second interface, _ iterator_ . This is the actual thing that iterates.
670
682
It has a ` next ` method that returns the next result. That result
671
683
should be an object with a ` value ` property, providing the next value,
672
- and a ` done ` property, which should be true when there are no more
673
- results and false otherwise.
684
+ if there is one, and a ` done ` property which should be true when there
685
+ are no more results and false otherwise.
674
686
675
687
Note that the ` next ` , ` value ` , and ` done ` property names are plain
676
688
strings, not symbols. Only ` Symbol.iterator ` , which is likely to be
@@ -697,14 +709,14 @@ class, acting as a two-dimensional array.
697
709
698
710
``` {includeCode: true}
699
711
class Matrix {
700
- constructor(width, height, content = (x, y) => undefined) {
712
+ constructor(width, height, element = (x, y) => undefined) {
701
713
this.width = width;
702
714
this.height = height;
703
715
this.content = [];
704
716
705
717
for (let y = 0; y < height; y++) {
706
718
for (let x = 0; x < width; x++) {
707
- this.content[y * width + x] = content (x, y);
719
+ this.content[y * width + x] = element (x, y);
708
720
}
709
721
}
710
722
}
@@ -895,10 +907,10 @@ the old class.
895
907
896
908
``` {includeCode: "top_lines: 17"}
897
909
class SymmetricMatrix extends Matrix {
898
- constructor(size, content = (x, y) => undefined) {
910
+ constructor(size, element = (x, y) => undefined) {
899
911
super(size, size, (x, y) => {
900
- if (x < y) return content (y, x);
901
- else return content (x, y);
912
+ if (x < y) return element (y, x);
913
+ else return element (x, y);
902
914
});
903
915
}
904
916
@@ -916,7 +928,7 @@ console.log(matrix.get(2, 3));
916
928
```
917
929
918
930
The use of the word ` extends ` indicates that this class shouldn't be
919
- based on the default ` Object ` prototype, but on some other class. This
931
+ directly based on the default ` Object ` prototype, but on some other class. This
920
932
is called the _ ((superclass))_ . The derived class is the
921
933
_ ((subclass))_ .
922
934
0 commit comments