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/04-object-basics/04-object-methods/2-check-syntax/solution.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,4 +34,4 @@ let user = {
34
34
(user.go)() // John
35
35
```
36
36
37
-
Please note that brackets around `(user.go)` do nothing here. Usually they setup the order of operations, but here the dot `.` works first anyway, so there's no effect. Only the semicolon thing matters.
37
+
Please note that parentheses around `(user.go)` do nothing here. Usually they setup the order of operations, but here the dot `.` works first anyway, so there's no effect. Only the semicolon thing matters.
Copy file name to clipboardExpand all lines: 1-js/09-classes/01-class/article.md
+87-4Lines changed: 87 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -298,13 +298,17 @@ class User {
298
298
newUser().sayHi();
299
299
```
300
300
301
-
## Class properties
301
+
## Class fields
302
302
303
303
```warn header="Old browsers may need a polyfill"
304
-
Class-level properties are a recent addition to the language.
304
+
Class fields are a recent addition to the language.
305
305
```
306
306
307
-
In the example above, `User` only had methods. Let's add a property:
307
+
Previously, classes only had methods.
308
+
309
+
"Class fields" is a syntax that allows to add any properties.
310
+
311
+
For instance, let's add `name` property to `class User`:
308
312
309
313
```js run
310
314
classUser {
@@ -323,7 +327,86 @@ alert(User.prototype.sayHi); // placed in User.prototype
323
327
alert(User.prototype.name); // undefined, not placed in User.prototype
324
328
```
325
329
326
-
The property `name` is not placed into `User.prototype`. Instead, it is created by `new` before calling the constructor, it's a property of the object itself.
330
+
The important thing about class fields is that they are set on individual objects, not `User.prototype`.
331
+
332
+
Technically, they are processed after the constructor has done it's job.
333
+
334
+
### Making bound methods with class fields
335
+
336
+
As demonstrated in the chapter <info:bind> functions in JavaScript have a dynamic `this`. It depends on the context of the call.
337
+
338
+
So if an object method is passed around and called in another context, `this` won't be a reference to its object any more.
339
+
340
+
For instance, this code will show `undefined`:
341
+
342
+
```js run
343
+
classButton {
344
+
constructor(value) {
345
+
this.value= value;
346
+
}
347
+
348
+
click() {
349
+
alert(this.value);
350
+
}
351
+
}
352
+
353
+
let button =newButton("hello");
354
+
355
+
*!*
356
+
setTimeout(button.click, 1000); // undefined
357
+
*/!*
358
+
```
359
+
360
+
The problem is called "losing `this`".
361
+
362
+
There are two approaches to fixing it, as discussed in the chapter <info:bind>:
363
+
364
+
1. Pass a wrapper-function, such as `setTimeout(() => button.click(), 1000)`.
365
+
2. Bind the method to object, e.g. in the constructor:
366
+
367
+
```js run
368
+
classButton {
369
+
constructor(value) {
370
+
this.value= value;
371
+
*!*
372
+
this.click=this.click.bind(this);
373
+
*/!*
374
+
}
375
+
376
+
click() {
377
+
alert(this.value);
378
+
}
379
+
}
380
+
381
+
let button =newButton("hello");
382
+
383
+
*!*
384
+
setTimeout(button.click, 1000); // hello
385
+
*/!*
386
+
```
387
+
388
+
Class fields provide a more elegant syntax for the latter solution:
389
+
390
+
```js run
391
+
classButton {
392
+
constructor(value) {
393
+
this.value= value;
394
+
}
395
+
*!*
396
+
click= () => {
397
+
alert(this.value);
398
+
}
399
+
*/!*
400
+
}
401
+
402
+
let button =newButton("hello");
403
+
404
+
setTimeout(button.click, 1000); // hello
405
+
```
406
+
407
+
The class field `click = () => {...}` creates an independent function on each `Button` object, with `this` bound to the object. Then we can pass `button.click` around anywhere, and it will be called with the right `this`.
408
+
409
+
That's especially useful in browser environment, when we need to setup a method as an event listener.
Copy file name to clipboardExpand all lines: 1-js/99-js-misc/03-currying-partials/article.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,7 @@ alert( curriedSum(1)(2) ); // 3
39
39
As you can see, the implementation is straightforward: it's just two wrappers.
40
40
41
41
- The result of `curry(func)` is a wrapper `function(a)`.
42
-
- When it is called like `sum(1)`, the argument is saved in the Lexical Environment, and a new wrapper is returned `function(b)`.
42
+
- When it is called like `curriedSum(1)`, the argument is saved in the Lexical Environment, and a new wrapper is returned `function(b)`.
43
43
- Then this wrapper is called with `2` as an argument, and it passes the call to the original `sum`.
44
44
45
45
More advanced implementations of currying, such as [_.curry](https://lodash.com/docs#curry) from lodash library, return a wrapper that allows a function to be called both normally and partially:
2.**`crossorigin="anonymous"`** -- access allowed if the server responds with the header `Access-Control-Allow-Origin` with `*` or our origin. Browser does not send authorization information and cookies to remote server.
172
-
3.**`crossorigin="use-credentials"`** -- access allowed if the server sends back the header `Access-Control-Allow-Origin` with our origin and `Access-Control-Allow-Credentials: true`. Browser sends authorization information and cookies to remote server.
172
+
3.**`crossorigin="use-credentials"`** -- access allowed if the server sends back the header `Access-Control-Allow-Origin` with our origin and `Access-Control-Allow-Credentials: true`. Browser sends authorization information and cookies to remote server.
173
173
174
174
```smart
175
175
You can read more about cross-origin access in the chapter <info:fetch-crossorigin>. It describes the `fetch` method for network requests, but the policy is exactly the same.
0 commit comments