Skip to content

Commit 8147267

Browse files
committed
Check conflicts.
1 parent b1f6bfb commit 8147267

File tree

1 file changed

+20
-40
lines changed

1 file changed

+20
-40
lines changed

1-js/09-classes/01-class/article.md

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MyClass {
2525
}
2626
```
2727

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.
2929

3030
The `constructor()` method is called automatically by `new`, so we can initialize the object there.
3131

@@ -53,7 +53,7 @@ When `new User("John")` is called:
5353
1. A new object is created.
5454
2. The `constructor` runs with the given argument and assigns it to `this.name`.
5555

56-
...Then we can call methods, such as `user.sayHi`.
56+
...Then we can call object methods, such as `user.sayHi()`.
5757

5858

5959
```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,
6464

6565
## What is a class?
6666

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.
6868

6969
Let's unveil any magic and see what a class really is. That'll help in understanding many complex aspects.
7070

@@ -85,11 +85,9 @@ alert(typeof User); // function
8585
```
8686

8787
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`.
9188

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`.
9391

9492
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.
9593

@@ -99,7 +97,6 @@ We can illustrate the result of `class User` declaration as:
9997

10098
Here's the code to introspect it:
10199

102-
103100
```js run
104101
class User {
105102
constructor(name) { this.name = name; }
@@ -171,16 +168,15 @@ Still, there are important differences.
171168
```
172169
There are other differences, we'll see them soon.
173170
174-
2. Class methods are non-enumerable
171+
2. Class methods are non-enumerable.
175172
A class definition sets `enumerable` flag to `false` for all methods in the `"prototype"`.
176173
177174
That's good, because if we `for..in` over an object, we usually don't want its class methods.
178175
179-
3. Classes always `use strict`
176+
3. Classes always `use strict`.
180177
All code inside the class construct is automatically in strict mode.
181178
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.
184180

185181
## Class Expression
186182

@@ -196,21 +192,22 @@ let User = class {
196192
};
197193
```
198194
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.
200196
201197
If a class expression has a name, it's visible inside the class only:
202198

203199
```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)
205202
let User = class *!*MyClass*/!* {
206203
sayHi() {
207-
alert(MyClass); // MyClass is visible only inside the class
204+
alert(MyClass); // MyClass name is visible only inside the class
208205
}
209206
};
210207
211208
new User().sayHi(); // works, shows MyClass definition
212209
213-
alert(MyClass); // error, MyClass not visible outside of the class
210+
alert(MyClass); // error, MyClass name isn't visible outside of the class
214211
```
215212

216213
We can even make classes dynamically "on-demand", like this:
@@ -243,7 +240,7 @@ class User {
243240
244241
constructor(name) {
245242
// invokes the setter
246-
this._name = name;
243+
this.name = name;
247244
}
248245
249246
*!*
@@ -277,10 +274,11 @@ Technically, such class declaration works by creating getters and setters in `Us
277274
Here's an example with a computed method name using brackets `[...]`:
278275

279276
```js run
280-
function f() { return "sayHi"; }
281-
282277
class User {
283-
[f()]() {
278+
279+
*!*
280+
['say' + 'Hi']() {
281+
*/!*
284282
alert("Hello");
285283
}
286284
@@ -405,29 +403,11 @@ That's especially useful in browser environment, for event listeners.
405403

406404
## Summary
407405

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-
function User(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-
426406
The basic class syntax looks like this:
427407

428408
```js
429409
class MyClass {
430-
prop = value; // field
410+
prop = value; // property
431411

432412
constructor(...) { // constructor
433413
// ...
@@ -438,7 +418,7 @@ class MyClass {
438418
get something(...) {} // getter method
439419
set something(...) {} // setter method
440420

441-
[Symbol.iterator]() {} // method with computed name/symbol name
421+
[Symbol.iterator]() {} // method with computed name (symbol here)
442422
// ...
443423
}
444424
```

0 commit comments

Comments
 (0)