Skip to content

Commit b78f329

Browse files
committed
work
1 parent 03cbee3 commit b78f329

File tree

7 files changed

+84
-4
lines changed

7 files changed

+84
-4
lines changed

1-js/2-first-steps/14-function-basics/article.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,22 @@ alert( doNothing() === undefined ); // true
318318
```
319319
````
320320
321+
````warn header="Never line-break between `return` and its value"
322+
For long expressions, it may be tempting sometimes to put them on a separate line, like this:
323+
324+
```js
325+
return
326+
(some + long + expression + or + whatever * f(a) + f(b))
327+
```
328+
That doesn't work, because Javascript assumes a semicolon after `return` in that case:
329+
330+
```js
331+
return*!*;*/!*
332+
(some + long + expression + or + whatever * f(a) + f(b))
333+
```
334+
So, it effectively becomes an empty return. We should put the value on the same line instead.
335+
````
336+
321337
## Naming a function [#function-naming]
322338
323339
Functions are actions. So their name is usually a verb. It should briefly, but as accurately as possible describe what the function does. So that a person who reads the code gets the right clue.
27.5 KB
Loading
65.8 KB
Loading

1-js/9-object-inheritance/05-class/article.md

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,10 +577,74 @@ Static methods are often used in database-related classes to search/save/remove
577577

578578
### Static methods and inheritance
579579

580-
Todo: picture with function -> prototype and vertical link for functions
580+
The `class` syntax supports inheritance for static properties too.
581581

582+
For instance:
582583

583-
## Todo absent constructor
584+
```js run
585+
class Animal {
586+
587+
constructor(name, speed) {
588+
this.speed = speed;
589+
this.name = name;
590+
}
591+
592+
run(speed = 0) {
593+
this.speed += speed;
594+
alert(`${this.name} runs with speed ${this.speed}.`);
595+
}
596+
597+
static compare(animalA, animalB) {
598+
return animalA.speed - animalB.speed;
599+
}
600+
601+
}
602+
603+
// Inherit from Animal
604+
class Rabbit extends Animal {
605+
hide() {
606+
alert(`${this.name} hides!`);
607+
}
608+
}
609+
610+
let rabbits = [
611+
new Rabbit("White Rabbit", 10),
612+
new Rabbit("Black Rabbit", 5)
613+
];
614+
615+
rabbits.sort(Rabbit.compare);
616+
617+
rabbits[0].run(); // Black Rabbit runs with speed 5.
618+
```
619+
620+
That's actually a very interesting feature, because built-in classes don't behave like that.
621+
622+
For instance, `Object` has `Object.defineProperty`, `Object.keys` and so on, but other objects do not inherit them.
623+
624+
Here's the structure for `Date` and `Object`:
625+
626+
![](object-date-inheritance.png)
627+
628+
Both `Object` and `Date` exist independently. Sure, `Date.prototype` inherits from `Object.prototype`, but that's all.
629+
630+
With classes we have one more arrow:
631+
632+
![](animal-rabbit-static.png)
633+
634+
Right, `Rabbit` function now inherits from `Animal` function. And `Animal` function standartly inherits from `Function.prototype` (as other functions do).
635+
636+
Here, let's check:
637+
638+
639+
```js run
640+
class Animal {}
641+
class Rabbit extends Animal {}
642+
643+
alert(Rabbit.__proto__ == Animal); // true
644+
645+
// and the next step is Function.prototype
646+
alert(Animal.__proto__ == Function.prototype); // true
647+
```
648+
649+
This way `Rabbit` has access to all static methods of `Animal`.
584650

585-
for simple classes parse `constructor( ){ }`
586-
for derived `constructor(... args){ super (...args);}`
33.6 KB
Loading
83.4 KB
Loading

figures.sketch

64 KB
Binary file not shown.

0 commit comments

Comments
 (0)