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
Copy file name to clipboardExpand all lines: 1-js/09-classes/02-class/article.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
2
2
# Classes
3
3
4
-
The "class" construct allows to define prototype-based classes with a clean, nice-looking syntax. It also introduces new great features, useful for object-oriented programming.
4
+
The "class" construct allows one to define prototype-based classes with a clean, nice-looking syntax. It also introduces great new features which are useful for object-oriented programming.
5
5
6
6
## The "class" syntax
7
7
@@ -22,7 +22,7 @@ let user = new User("John");
22
22
user.sayHi();
23
23
```
24
24
25
-
...And that's the same using `class` syntax:
25
+
...And here's the same using `class` syntax:
26
26
27
27
```js run
28
28
classUser {
@@ -41,17 +41,17 @@ let user = new User("John");
41
41
user.sayHi();
42
42
```
43
43
44
-
It's easy to see that the two examples are alike. Just please note that methods in a class do not have a comma between them. Novice developers sometimes forget it and put a comma between class methods, and things don't work. That's not a literal object, but a class syntax.
44
+
It's easy to see that these two examples are alike. Be sure to note that methods in a class do not have a comma between them. A common pitfall for novice developers is to put a comma between class methods, which would result in a syntax error. The above are not actually objects but simply making use of a class syntax. This notation is primarily syntactical sugar.
45
45
46
46
## What is a class?
47
47
48
48
So, what exactly is a `class`? We may think that it defines a new language-level entity, but that would be wrong.
49
49
50
50
In Javascript, a class is a kind of a function.
51
51
52
-
The definition `class User {...}`create such function and puts the methods into `User.prototype`. So the structure is similar.
52
+
The definition `class User {...}`creates a function under the same name and puts the methods into `User.prototype`. So the structure is similar.
53
53
54
-
Here's the code to dig into the class and see that:
54
+
This is demonstrated in the following code, which you can run yourself:
Here's the illustration of what `class User`creates:
78
+
Abstractly, we can illustrate this process of `class User`creating a function as:
79
79
80
80

81
81
82
-
So `class` is a special syntax to define a constructor together with its prototype methods. There are also quite a few additional features here and there, we'll study them later on.
82
+
`Class` is a special syntax to define a constructor together with its prototype methods. In addition to its basic operation, the `Class` syntax brings many other features with it which we'll explore later.
83
83
84
84
## Class Expression
85
85
86
86
Just like functions, classes can be defined inside another expression, passed around, returned etc.
87
87
88
-
Here's a class-returning function ("class factory"):
88
+
Here's a class-returning function - otherwise known as a "class factory":
alert(MyClass); // error, MyClass not visible outside of the class
122
122
```
123
123
124
-
## Differences of classes vs functions
124
+
## Differences between classes and functions
125
125
126
126
Classes have some differences compared to regular functions:
127
127
@@ -146,7 +146,7 @@ Class methods are non-enumerable
146
146
: A class definition sets `enumerable` flag to `false` for all methods in the `"prototype"`. That's good, because if we `for..in` over an object, we usually don't want its class methods.
147
147
148
148
Classes have a default `constructor() {}`
149
-
: If there's no `constructor` in the `class` construct, then an empty function is generated, same as if we had written `constructor() {}`.
149
+
: If there's no `constructor` in the `class` construct, then an empty function is generated, just as if we had written `constructor() {}`.
150
150
151
151
Classes always `use strict`
152
152
: All code inside the class construct is automatically in strict mode.
@@ -225,10 +225,10 @@ For a generator method, similarly, prepend it with `*`.
225
225
## Class properties
226
226
227
227
```warn header="Old browsers may need a polyfill"
228
-
Class-level properties is a recent addition to the language.
228
+
Class-level properties are a recent addition to the language.
229
229
```
230
230
231
-
In the example before, `User` only had methods. Let's add a property:
231
+
In the example above, `User` only had methods. Let's add a property:
0 commit comments