You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
````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
+
321
337
## Naming a function [#function-naming]
322
338
323
339
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.
Copy file name to clipboardExpand all lines: 1-js/9-object-inheritance/05-class/article.md
+68-4Lines changed: 68 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -577,10 +577,74 @@ Static methods are often used in database-related classes to search/save/remove
577
577
578
578
### Static methods and inheritance
579
579
580
-
Todo: picture with function -> prototype and vertical link for functions
580
+
The `class` syntax supports inheritance for static properties too.
581
581
582
+
For instance:
582
583
583
-
## Todo absent constructor
584
+
```js run
585
+
classAnimal {
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
+
staticcompare(animalA, animalB) {
598
+
returnanimalA.speed-animalB.speed;
599
+
}
600
+
601
+
}
602
+
603
+
// Inherit from Animal
604
+
classRabbitextendsAnimal {
605
+
hide() {
606
+
alert(`${this.name} hides!`);
607
+
}
608
+
}
609
+
610
+
let rabbits = [
611
+
newRabbit("White Rabbit", 10),
612
+
newRabbit("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
+

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
+

633
+
634
+
Right, `Rabbit` function now inherits from `Animal` function. And `Animal` function standartly inherits from `Function.prototype` (as other functions do).
0 commit comments