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/01-class/article.md
+20-40Lines changed: 20 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ class MyClass {
25
25
}
26
26
```
27
27
28
-
Then `new MyClass()`creates a new object with all the listed methods.
28
+
Then use `new MyClass()`to create a new object with all the listed methods.
29
29
30
30
The `constructor()` method is called automatically by `new`, so we can initialize the object there.
31
31
@@ -53,7 +53,7 @@ When `new User("John")` is called:
53
53
1. A new object is created.
54
54
2. The `constructor` runs with the given argument and assigns it to `this.name`.
55
55
56
-
...Then we can call methods, such as `user.sayHi`.
56
+
...Then we can call object methods, such as `user.sayHi()`.
57
57
58
58
59
59
```warn header="No comma between class methods"
@@ -64,7 +64,7 @@ The notation here is not to be confused with object literals. Within the class,
64
64
65
65
## What is a class?
66
66
67
-
So, what exactly is a `class`? That's not an entirely new language-level entity, as one might think.
67
+
So, what exactly is a `class`? That's not an entirely new language-level entity, as one might think.
68
68
69
69
Let's unveil any magic and see what a class really is. That'll help in understanding many complex aspects.
70
70
@@ -85,11 +85,9 @@ alert(typeof User); // function
85
85
```
86
86
87
87
What `class User {...}` construct really does is:
88
-
1. Creates a function named `User`, that becomes the result of the class declaration.
89
-
- The function code is taken from the `constructor` method (assumed empty if we don't write such method).
90
-
3. Stores all methods, such as `sayHi`, in `User.prototype`.
91
88
92
-
Afterwards, for new objects, when we call a method, it's taken from the prototype, just as described in the chapter <info:function-prototype>. So `new User` object has access to class methods.
89
+
1. Creates a function named `User`, that becomes the result of the class declaration. The function code is taken from the `constructor` method (assumed empty if we don't write such method).
90
+
2. Stores class methods, such as `sayHi`, in `User.prototype`.
93
91
94
92
After `new User` object is created, when we call its method, it's taken from the prototype, just as described in the chapter <info:function-prototype>. So the object has access to class methods.
95
93
@@ -99,7 +97,6 @@ We can illustrate the result of `class User` declaration as:
99
97
100
98
Here's the code to introspect it:
101
99
102
-
103
100
```js run
104
101
classUser {
105
102
constructor(name) { this.name= name; }
@@ -171,16 +168,15 @@ Still, there are important differences.
171
168
```
172
169
There are other differences, we'll see them soon.
173
170
174
-
2. Class methods are non-enumerable
171
+
2. Class methods are non-enumerable.
175
172
A class definition sets `enumerable` flag to `false` for all methods in the `"prototype"`.
176
173
177
174
That's good, because if we `for..in` over an object, we usually don't want its class methods.
178
175
179
-
3. Classes always `use strict`
176
+
3. Classes always `use strict`.
180
177
All code inside the class construct is automatically in strict mode.
181
178
182
-
183
-
Also, in addition to its basic operation, the `class` syntax brings many other features with it which we'll explore later.
179
+
Besides, `class` syntax brings many other features that we'll explore later.
184
180
185
181
## Class Expression
186
182
@@ -196,21 +192,22 @@ let User = class {
196
192
};
197
193
```
198
194
199
-
Similar to Named Function Expressions, class expressions may or may not have a name.
195
+
Similar to Named Function Expressions, class expressions may have a name.
200
196
201
197
If a class expression has a name, it's visible inside the classonly:
202
198
203
199
```js run
204
-
// "Named Class Expression" (alas, no such term, but that's what's going on)
200
+
// "Named Class Expression"
201
+
// (no such term in the spec, but that's similar to Named Function Expression)
205
202
let User = class *!*MyClass*/!* {
206
203
sayHi() {
207
-
alert(MyClass); // MyClass is visible only inside the class
204
+
alert(MyClass); // MyClass name is visible only inside the class
208
205
}
209
206
};
210
207
211
208
new User().sayHi(); // works, shows MyClass definition
212
209
213
-
alert(MyClass); // error, MyClass not visible outside of the class
210
+
alert(MyClass); // error, MyClass name isn't visible outside of the class
214
211
```
215
212
216
213
We can even make classes dynamically "on-demand", like this:
@@ -243,7 +240,7 @@ class User {
243
240
244
241
constructor(name) {
245
242
// invokes the setter
246
-
this._name = name;
243
+
this.name = name;
247
244
}
248
245
249
246
*!*
@@ -277,10 +274,11 @@ Technically, such class declaration works by creating getters and setters in `Us
277
274
Here's an example with a computed method name using brackets `[...]`:
278
275
279
276
```js run
280
-
function f() { return "sayHi"; }
281
-
282
277
class User {
283
-
[f()]() {
278
+
279
+
*!*
280
+
['say' + 'Hi']() {
281
+
*/!*
284
282
alert("Hello");
285
283
}
286
284
@@ -405,29 +403,11 @@ That's especially useful in browser environment, for event listeners.
405
403
406
404
## Summary
407
405
408
-
JavaScript provides many ways to create a class.
409
-
410
-
First, as per the general object-oriented terminology, a class is something that provides "object templates", allows to create same-structured objects.
411
-
412
-
When we say "a class", that doesn't necessary means the `class` keyword.
413
-
414
-
This is a class:
415
-
416
-
```js
417
-
functionUser(name) {
418
-
this.sayHi=function() {
419
-
alert(name);
420
-
}
421
-
}
422
-
```
423
-
424
-
...But in most cases `class` keyword is used, as it provides great syntax and many additional features.
425
-
426
406
The basic class syntax looks like this:
427
407
428
408
```js
429
409
classMyClass {
430
-
prop = value; //field
410
+
prop = value; //property
431
411
432
412
constructor(...) { // constructor
433
413
// ...
@@ -438,7 +418,7 @@ class MyClass {
438
418
getsomething(...) {} // getter method
439
419
setsomething(...) {} // setter method
440
420
441
-
[Symbol.iterator]() {} // method with computed name/symbol name
421
+
[Symbol.iterator]() {} // method with computed name (symbol here)
0 commit comments