From 23ffde780605b3418101850a61d9496c3d7f927c Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Sat, 4 Apr 2020 15:15:09 +0300 Subject: [PATCH] minor fixes --- 1-js/09-classes/01-class/article.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/1-js/09-classes/01-class/article.md b/1-js/09-classes/01-class/article.md index dd2b390e..b495d45f 100644 --- a/1-js/09-classes/01-class/article.md +++ b/1-js/09-classes/01-class/article.md @@ -333,8 +333,6 @@ Technically, they are processed after the constructor has done it's job. ### Making bound methods with class fields -Class fields together with arrow functions can be used to create "bound" methods, with fixed `this` that always references the object. - As demonstrated in the chapter functions in JavaScript have a dynamic `this`. It depends on the context of the call. So if an object method is passed around and called in another context, `this` won't be a reference to its object any more. @@ -361,17 +359,17 @@ setTimeout(button.click, 1000); // undefined The problem is called "losing `this`". -There are two ways to fix it, as discussed in the chapter : +There are two approaches to fixing it, as discussed in the chapter : 1. Pass a wrapper-function, such as `setTimeout(() => button.click(), 1000)`. -2. Bind the method to object in the constructor: +2. Bind the method to object, e.g. in the constructor: ```js run class Button { constructor(value) { this.value = value; *!* - this.click = this.click.bound(this); + this.click = this.click.bind(this); */!* } @@ -406,7 +404,7 @@ let button = new Button("hello"); setTimeout(button.click, 1000); // hello ``` -As you can see, `click = () => {...}` creates an independent function on each `Button` object, with `this` bound to the object. +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`. That's especially useful in browser environment, when we need to setup a method as an event listener.