Skip to content

Commit bf6d3c9

Browse files
committed
minor fixes
1 parent 2cca9a9 commit bf6d3c9

File tree

1 file changed

+12
-8
lines changed
  • 1-js/09-classes/03-static-properties-methods

1 file changed

+12
-8
lines changed

1-js/09-classes/03-static-properties-methods/article.md

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
# Static properties and methods
33

4-
We can also assign a method to the class function itself, not to its `"prototype"`. Such methods are called *static*.
4+
We can also assign a method to the class as a whole. Such methods are called *static*.
55

6-
In a class, they are prepended by `static` keyword, like this:
6+
In a class declaration, they are prepended by `static` keyword, like this:
77

88
```js run
99
class User {
@@ -31,9 +31,11 @@ User.staticMethod(); // true
3131

3232
The value of `this` in `User.staticMethod()` call is the class constructor `User` itself (the "object before dot" rule).
3333

34-
Usually, static methods are used to implement functions that belong to the class, but not to any particular object of it.
34+
Usually, static methods are used to implement functions that belong to the class as a whole, but not to any particular object of it.
3535

36-
For instance, we have `Article` objects and need a function to compare them. A natural solution would be to add `Article.compare` method, like this:
36+
For instance, we have `Article` objects and need a function to compare them.
37+
38+
A natural solution would be to add `Article.compare` static method:
3739

3840
```js run
3941
class Article {
@@ -63,17 +65,19 @@ articles.sort(Article.compare);
6365
alert( articles[0].title ); // CSS
6466
```
6567

66-
Here `Article.compare` stands "above" articles, as a means to compare them. It's not a method of an article, but rather of the whole class.
68+
Here `Article.compare` method stands "above" articles, as a means to compare them. It's not a method of an article, but rather of the whole class.
69+
70+
Another example would be a so-called "factory" method.
6771

68-
Another example would be a so-called "factory" method. Imagine, we need few ways to create an article:
72+
Let's say, we need multiple ways to create an article:
6973

7074
1. Create by given parameters (`title`, `date` etc).
7175
2. Create an empty article with today's date.
7276
3. ...or else somehow.
7377

7478
The first way can be implemented by the constructor. And for the second one we can make a static method of the class.
7579

76-
Like `Article.createTodays()` here:
80+
Such as `Article.createTodays()` here:
7781

7882
```js run
7983
class Article {
@@ -101,7 +105,7 @@ Static methods are also used in database-related classes to search/save/remove e
101105

102106
```js
103107
// assuming Article is a special class for managing articles
104-
// static method to remove the article:
108+
// static method to remove the article by id:
105109
Article.remove({id: 12345});
106110
```
107111

0 commit comments

Comments
 (0)