URL: http://github.com/javascript-tutorial/bg.javascript.info/pull/39.patch
° Π΅ Π°ΡΡ ΠΈΡΠ΅ΠΊΡΡΡΠ° ΠΈ ΠΈΠΌΠ° ΠΌΠ½ΠΎΠ³ΠΎ ΠΊΠ½ΠΈΠ³ΠΈ, Π½Π°ΠΏΠΈΡΠ°Π½ΠΈ ΠΏΠΎ ΡΠΎΠ·ΠΈ Π²ΡΠΏΡΠΎΡ, ΠΊΠ°ΡΠΎ "Design Patterns: Elements of Reusable Object-Oriented Software" ΠΎΡ E. Gamma, R. Helm, R. Johnson, J. Vissides ΠΈΠ»ΠΈ "Object-Oriented Analysis and Design with Applications" ΠΎΡ G. Booch, ΠΈ ΠΎΡΠ΅. ``` ### Method shorthand ΠΠΌΠ° ΠΈ ΠΏΠΎ-ΠΊΡΠ°ΡΡΠΊ Π½Π°ΡΠΈΠ½ Π΄Π° ΡΠ΅ ΠΏΠΈΡΠ°Ρ ΠΌΠ΅ΡΠΎΠ΄ΠΈ Π² ΠΎΠ±Π΅ΠΊΡΠ΅Π½ Π»ΠΈΡΠ΅ΡΠ°Π»: ```js -// Π’Π΅Π·ΠΈ ΠΎΠ±Π΅ΠΊΡΠΈ ΠΏΡΠ°Π²ΡΡ Π΅Π΄Π½ΠΎ ΠΈ ΡΡΡΠΎ +// Π’Π΅Π·ΠΈ ΠΎΠ±Π΅ΠΊΡΠΈ ΠΏΡΠ°Π²ΡΡ Π΅Π΄Π½ΠΎ ΠΈ ΡΡΡΠΎ Π½Π΅ΡΠΎ user = { sayHi: function() { @@ -78,7 +78,7 @@ user = { } }; -// ΠΊΡΠ°ΡΠΊΠΈΡ Π½Π°ΡΠΈΠ½ ΠΈΠ·Π³Π»Π΅ΠΆΠ΄Π° ΠΏΠΎ-Π΄ΠΎΠ±ΡΠ΅, Π½Π°Π»ΠΈ? +// ΠΡΠ°ΡΠΊΠΈΡΡ Π½Π°ΡΠΈΠ½ ΠΈΠ·Π³Π»Π΅ΠΆΠ΄Π° ΠΏΠΎ-Π΄ΠΎΠ±ΡΠ΅, Π½Π°Π»ΠΈ? user = { *!* sayHi() { // ΡΡΡΠΎ ΠΊΠ°ΡΠΎ "sayHi: function()" @@ -88,21 +88,21 @@ user = { }; ``` -ΠΠ°ΠΊΡΠΎ Π²ΠΈΠ΄ΡΡ ΠΌΠ΅, ΠΌΠΎΡΠ΅ Π΄Π° ΠΏΡΠΎΠΏΡΡΠ½Π΅ΠΌ `"function"` ΠΈ Π΄Π° Π½Π°ΠΏΠΈΡΠ΅ΠΌ ΡΠ°ΠΌΠΎ `sayHi()`. +ΠΠ°ΠΊΡΠΎ Π²ΠΈΠ΄ΡΡ ΠΌΠ΅, ΠΌΠΎΠΆΠ΅ Π΄Π° ΠΏΡΠΎΠΏΡΡΠ½Π΅ΠΌ `"function"` ΠΈ Π΄Π° Π½Π°ΠΏΠΈΡΠ΅ΠΌ ΡΠ°ΠΌΠΎ `sayHi()`. -To tell the truth, the notations are not fully identical. There are subtle differences related to object inheritance (to be covered later), but for now they do not matter. In almost all cases the shorter syntax is preferred. +Π Π΄Π΅ΠΉΡΡΠ²ΠΈΡΠ΅Π»Π½ΠΎΡΡ ΡΠ΅Π·ΠΈ Π½ΠΎΡΠ°ΡΠΈΠΈ Π½Π΅ ΡΠ° Π½Π°ΠΏΡΠ»Π½ΠΎ ΠΈΠ΄Π΅Π½ΡΠΈΡΠ½ΠΈ. ΠΠΌΠ° Π»Π΅ΠΊΠΈ ΡΠ°Π·Π»ΠΈΡΠΈΡ, ΡΠ²ΡΡΠ·Π°Π½ΠΈ Ρ ΠΎΠ±Π΅ΠΊΡΠ½ΠΎΡΠΎ ΡΠ½Π°ΡΠ»Π΅Π΄ΡΠ²Π°Π½Π΅ (ΡΠ΅ Π±ΡΠ΄Π΅ ΡΡΠ·Π³Π»Π΅Π΄Π°Π½ΠΎ ΠΏΠΎ-ΠΊΡΡΠ½ΠΎ), Π½ΠΎ Π·Π° ΡΠ΅Π³Π° ΡΠΎΠ²Π° Π½ΡΠΌΠ° Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅. Π ΠΏΠΎΡΡΠΈ Π²ΡΠ΅ΠΊΠΈ ΡΠ»ΡΡΠ°ΠΉ ΡΠ΅ ΠΏΡΠ΅Π΄ΠΏΠΎΡΠΈΡΠ° ΠΊΡΠ°ΡΠΊΠΈΡΡ ΡΠΈΠ½ΡΠ°ΠΊΡΠΈΡ. -## "this" in methods +## "this" Π² ΠΌΠ΅ΡΠΎΠ΄ΠΈΡΠ΅ -It's common that an object method needs to access the information stored in the object to do its job. +ΠΠ΅ΡΠΎΠ΄ΡΡ Π½Π° ΠΎΠ±Π΅ΠΊΡΠ° ΠΈΡΠΊΠ° Π΄Π° Π΄ΠΎΡΡΡΠΏΠΈ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡΡΠ°, ΠΊΠΎΡΡΠΎ ΡΠ΅ ΡΡΡ ΡΠ°Π½ΡΠ²Π° Π² ΠΎΠ±Π΅ΠΊΡΠ°, Π·Π° Π΄Π° ΡΠΈ ΡΠ²ΡΡΡΠΈ ΡΠ°Π±ΠΎΡΠ°ΡΠ°. -For instance, the code inside `user.sayHi()` may need the name of the `user`. +ΠΠ°ΠΏΡΠΈΠΌΠ΅Ρ, ΠΊΠΎΠ΄ΡΡ Π²ΡΡΡΠ΅ Π² `user.sayHi()` ΠΌΠΎΠΆΠ΅ Π΄Π° ΠΈΠΌΠ° Π½ΡΠΆΠ΄Π° ΠΎΡ ΠΈΠΌΠ΅ΡΠΎ Π½Π° `user`. -**To access the object, a method can use the `this` keyword.** +**ΠΠ° Π΄Π° Π΄ΠΎΡΡΡΠΏΠΈ ΠΎΠ±Π΅ΠΊΡΠ°, ΠΌΠ΅ΡΠΎΠ΄ΡΡ ΠΌΠΎΠΆΠ΅ Π΄Π° ΠΈΠ·ΠΏΠΎΠ»Π·Π²Π° ΠΊΠ»ΡΡΠΎΠ²Π°ΡΠ° Π΄ΡΠΌΠ° `this`.** -The value of `this` is the object "before dot", the one used to call the method. +Π‘ΡΠΎΠΉΠ½ΠΎΡΡΡΠ° Π½Π° `this` Π΅ ΠΎΠ±Π΅ΠΊΡΡΡ "ΠΏΡΠ΅Π΄ΠΈ ΡΠΎΡΠΊΠ°ΡΠ°", ΡΠΎΠ·ΠΈ, ΠΊΠΎΠΉΡΠΎ ΡΠ΅ ΠΈΠ·ΠΏΠΎΠ»Π·Π²Π°, Π·Π° Π΄Π° ΡΠ΅ ΠΈΠ·Π²ΠΈΠΊΠ° ΠΌΠ΅ΡΠΎΠ΄Π°. -For instance: +ΠΠ°ΠΏΡΠΈΠΌΠ΅Ρ: ```js run let user = { @@ -111,7 +111,7 @@ let user = { sayHi() { *!* - // "this" is the "current object" + // "this" Π΅ "ΡΠ΅ΠΊΡΡΠΈΡΡ ΠΎΠ±Π΅ΠΊΡ" alert(this.name); */!* } @@ -121,9 +121,9 @@ let user = { user.sayHi(); // John ``` -Here during the execution of `user.sayHi()`, the value of `this` will be `user`. +Π’ΡΠΊ ΠΏΠΎ Π²ΡΠ΅ΠΌΠ΅ Π½Π° ΠΈΠ·ΠΏΡΠ»Π½Π΅Π½ΠΈΠ΅ΡΠΎ Π½Π° `user.sayHi()`, ΡΡΠΎΠΉΠ½ΠΎΡΡΡΠ° Π½Π° `this` ΡΠ΅ Π±ΡΠ΄Π΅ `user`. -Technically, it's also possible to access the object without `this`, by referencing it via the outer variable: +Π’Π΅Ρ Π½ΠΈΡΠ΅ΡΠΊΠΈ Π΅ Π²ΡΠ·ΠΌΠΎΠΆΠ½ΠΎ Π΄Π° Π΄ΠΎΡΡΡΠΏΠΈΠΌ ΠΎΠ±Π΅ΠΊΡΠ° ΠΈ Π±Π΅Π· `this`, ΠΊΠ°ΡΠΎ Π³ΠΎ ΡΠ΅ΡΠ΅ΡΠΈΡΠ°ΠΌΠ΅ ΠΏΡΠ΅Π· Π²ΡΠ½ΡΠ½Π°ΡΠ° ΠΏΡΠΎΠΌΠ΅Π½Π»ΠΈΠ²Π°: ```js let user = { @@ -132,16 +132,16 @@ let user = { sayHi() { *!* - alert(user.name); // "user" instead of "this" + alert(user.name); // "user" Π²ΠΌΠ΅ΡΡΠΎ "this" */!* } }; ``` -...But such code is unreliable. If we decide to copy `user` to another variable, e.g. `admin = user` and overwrite `user` with something else, then it will access the wrong object. +...ΠΠΎ ΡΠΎΠ·ΠΈ ΠΊΠΎΠ΄ Π½Π΅ Π΅ Π½Π°Π΄Π΅ΠΆΠ΄Π΅Π½. ΠΠΊΠΎ ΡΠ΅ΡΠΈΠΌ Π΄Π° ΠΊΠΎΠΏΠΈΡΠ°ΠΌΠ΅ `user` Π² Π΄ΡΡΠ³Π° ΠΏΡΠΎΠΌΠ΅Π½Π»ΠΈΠ²Π°, ΠΏΡ. `admin = user` ΠΈ ΠΏΡΠ΅Π·Π°ΠΏΠΈΡΠ΅ΠΌ `user` Ρ Π½Π΅ΡΠΎ Π΄ΡΡΠ³ΠΎ, ΡΠΎΠ³Π°Π²Π° ΡΠ΅ Π΄ΠΎΡΡΡΠΏΠΈΠΌ Π³ΡΠ΅ΡΠ½ΠΈΡ ΠΎΠ±Π΅ΠΊΡ. -That's demonstrated below: +Π’ΠΎΠ²Π° Π΅ ΠΏΠΎΠΊΠ°Π·Π°Π½ΠΎ Π΄ΠΎΠ»Ρ: ```js run let user = { @@ -150,7 +150,7 @@ let user = { sayHi() { *!* - alert( user.name ); // leads to an error + alert( user.name ); // Π²ΠΎΠ΄ΠΈ ΠΊΡΠΌ Π³ΡΠ΅ΡΠΊΠ° */!* } @@ -158,18 +158,18 @@ let user = { let admin = user; -user = null; // overwrite to make things obvious +user = null; // ΠΏΡΠ΅Π·Π°ΠΏΠΈΡΠ²Π°ΠΌΠ΅ Π·Π° Π΄Π° Π½Π°ΠΏΡΠ°Π²ΠΈΠΌ Π½Π΅ΡΠ°ΡΠ° ΠΏΠΎ-ΡΡΠ½ΠΈ -admin.sayHi(); // Whoops! inside sayHi(), the old name is used! error! +admin.sayHi(); // ΠΠΏΠ°! ΠΡΡΡΠ΅ Π² sayHi(), ΡΠ΅ ΠΈΠ·ΠΏΠΎΠ»Π·Π²Π° ΡΡΠ°ΡΠΎΡΠΎ ΠΈΠΌΠ΅! ΠΡΠ΅ΡΠΊΠ°! ``` -If we used `this.name` instead of `user.name` inside the `alert`, then the code would work. +ΠΠΊΠΎ ΠΈΠ·ΠΏΠΎΠ»Π·Π²Π°Ρ ΠΌΠ΅ `this.name` Π²ΠΌΠ΅ΡΡΠΎ `user.name` Π²ΡΡΡΠ΅ Π² `alert`, ΠΊΠΎΠ΄ΡΡ ΡΠ΅ΡΠ΅ Π΄Π° ΡΠ°Π±ΠΎΡΠΈ. -## "this" is not bound +## "this" Π½Π΅ Π΅ ΠΎΠ±Π²ΡΡΠ·Π°Π½ -In JavaScript, keyword `this` behaves unlike most other programming languages. It can be used in any function. +Π JavaScript, ΠΊΠ»ΡΡΠΎΠ²Π°ΡΠ° Π΄ΡΠΌΠ° `this` ΡΠ΅ Π΄ΡΡΠΆΠΈ ΡΠ°Π·Π»ΠΈΡΠ½ΠΎ Π² ΡΡΠ°Π²Π½Π΅Π½ΠΈΠ΅ Ρ ΠΏΠΎΠ²Π΅ΡΠ΅ΡΠΎ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠ½ΠΈ Π΅Π·ΠΈΡΠΈ. Π’Ρ ΠΌΠΎΠΆΠ΅ Π΄Π° ΡΠ΅ ΠΈΠ·ΠΏΠΎΠ»Π·Π²Π° Π²ΡΠ² Π²ΡΡΠΊΠ° ΡΡΠ½ΠΊΡΠΈΡ. -There's no syntax error in the following example: +ΠΡΠΌΠ° ΡΠΈΠ½ΡΠ°ΠΊΡΠΈΡΠ½Π° Π³ΡΠ΅ΡΠΊΠ° Π² ΡΠ»Π΅Π΄Π²Π°ΡΠΈΡ ΠΏΡΠΈΠΌΠ΅Ρ: ```js function sayHi() { @@ -177,9 +177,9 @@ function sayHi() { } ``` -The value of `this` is evaluated during the run-time, depending on the context. +Π‘ΡΠΎΠΉΠ½ΠΎΡΡΡΠ° Π½Π° `this` ΡΠ΅ ΠΈΠ·ΡΠΈΡΠ»ΡΠ²Π° ΠΏΠΎ Π²ΡΠ΅ΠΌΠ΅ Π½Π° ΠΈΠ·ΠΏΡΠ»Π½Π΅Π½ΠΈΠ΅ Π½Π° ΠΊΠΎΠ΄Π° Π² Π·Π°Π²ΠΈΡΠΈΠΌΠΎΡΡ ΠΎΡ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°. -For instance, here the same function is assigned to two different objects and has different "this" in the calls: +ΠΠ°ΠΏΡΠΈΠΌΠ΅Ρ ΡΡΠΊ Π΅Π΄Π½Π° ΠΈ ΡΡΡΠ° ΡΡΠ½ΠΊΡΠΈΡ Π΅ ΠΏΡΠΈΡΠ²ΠΎΠ΅Π½Π° Π½Π° Π΄Π²Π° ΡΠ°Π·Π»ΠΈΡΠ½ΠΈ ΠΎΠ±Π΅ΠΊΡΠ° ΠΈ ΠΈΠΌΠ° ΡΠ°Π·Π»ΠΈΡΠ½ΠΈ "this" ΠΏΠΎ Π²ΡΠ΅ΠΌΠ΅ Π½Π° ΠΈΠ·Π²ΠΈΠΊΠ²Π°Π½Π΅: ```js run let user = { name: "John" }; @@ -190,23 +190,23 @@ function sayHi() { } *!* -// use the same function in two objects +// ΠΠ·ΠΏΠΎΠ»Π·Π²Π°ΠΌΠ΅ Π΅Π΄Π½Π° ΠΈ ΡΡΡΠ° ΡΡΠ½ΠΊΡΠΈΡ Π² Π΄Π²Π° ΠΎΠ±Π΅ΠΊΡΠ° user.f = sayHi; admin.f = sayHi; */!* -// these calls have different this -// "this" inside the function is the object "before the dot" +// ΡΠ΅Π·ΠΈ ΠΈΠ·Π²ΠΈΠΊΠ²Π°Π½ΠΈΡ ΠΈΠΌΠ°Ρ ΡΠ°Π·Π»ΠΈΡΠ½ΠΈ this +// "this" Π²ΡΡΡΠ΅ Π²ΡΠ² ΡΡΠ½ΠΊΡΠΈΡΡΠ° Π΅ ΠΎΠ±Π΅ΠΊΡΠ° "ΠΏΡΠ΅Π΄ΠΈ ΡΠΎΡΠΊΠ°ΡΠ°" user.f(); // John (this == user) admin.f(); // Admin (this == admin) -admin['f'](); // Admin (dot or square brackets access the method β doesn't matter) +admin['f'](); // Admin (ΡΠΎΡΠΊΠ° ΠΈΠ»ΠΈ ΠΊΠ²Π°Π΄ΡΠ°ΡΠ½ΠΈ ΡΠΊΠΎΠ±ΠΈ Π΄ΠΎΡΡΡΠΏΠ²Π° ΠΌΠ΅ΡΠΎΠ΄Π° β Π½ΡΠΌΠ° Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅) ``` -The rule is simple: if `obj.f()` is called, then `this` is `obj` during the call of `f`. So it's either `user` or `admin` in the example above. +ΠΡΠ°Π²ΠΈΠ»ΠΎΡΠΎ Π΅ ΠΏΡΠΎΡΡΠΎ: Π°ΠΊΠΎ ΠΈΠ·Π²ΠΈΠΊΠ°ΠΌΠ΅ `obj.f()`, ΡΠΎΠ³Π°Π²Π° `this` Π΅ `obj` ΠΏΠΎ Π²ΡΠ΅ΠΌΠ΅ Π½Π° ΠΈΠ·Π²ΠΈΠΊΠ²Π°Π½Π΅ΡΠΎ ΠΎΡ `f`. Π’Π°ΠΊΠ° ΡΠ΅ ΡΠΎΠ²Π° Π΅ ΠΈΠ»ΠΈ `user` ΠΈΠ»ΠΈ `admin` Π² Π³ΠΎΡΠ½ΠΈΡ ΠΏΡΠΈΠΌΠ΅Ρ. -````smart header="Calling without an object: `this == undefined`" -We can even call the function without an object at all: +````smart header="ΠΠ·Π²ΠΈΠΊΠ²Π°Π½Π΅ Π±Π΅Π· ΠΎΠ±Π΅ΠΊΡ: `this == undefined`" +ΠΠΈΠ΅ ΠΌΠΎΠΆΠ΅ Π΄Π° ΠΈΠ·Π²ΠΈΠΊΠ°ΠΌΠ΅ ΡΡΠ½ΠΊΡΠΈΡΡΠ° Π΄ΠΎΡΠΈ Π±Π΅Π· ΠΎΠ±Π΅ΠΊΡΠ°: ```js run function sayHi() { @@ -216,28 +216,28 @@ function sayHi() { sayHi(); // undefined ``` -In this case `this` is `undefined` in strict mode. If we try to access `this.name`, there will be an error. +Π ΡΠΎΠ·ΠΈ ΡΠ»ΡΡΠ°ΠΉ `this` Π΅ `undefined` Π² ΡΡΡΠΈΠΊΡΠ΅Π½ ΡΠ΅ΠΆΠΈΠΌ. ΠΠΊΠΎ ΠΎΠΏΠΈΡΠ°ΠΌΠ΅ Π΄Π° Π΄ΠΎΡΡΡΠΏΠΈΠΌ `this.name`, ΡΠ΅ ΠΈΠΌΠ° Π³ΡΠ΅ΡΠΊΠ°. -In non-strict mode the value of `this` in such case will be the *global object* (`window` in a browser, we'll get to it later in the chapter [](info:global-object)). This is a historical behavior that `"use strict"` fixes. +Π Π½Π΅ΡΡΡΠΈΠΊΡΠ΅Π½ ΡΠ΅ΠΆΠΈΠΌ ΡΡΠΎΠΉΠ½ΠΎΡΡΡΠ° Π½Π° `this` Π² ΡΠΎΠ·ΠΈ ΡΠ»ΡΡΠ°ΠΉ ΡΠ΅ Π±ΡΠ΄Π΅ *Π³Π»ΠΎΠ±Π°Π»Π½ΠΈΡΡ ΠΎΠ±Π΅ΠΊΡ* (`window` Π² Π±ΡΠ°ΡΠ·ΡΡΠ°, ΡΠ΅ Π³ΠΎ ΡΠ°Π·Π³Π»Π΅Π΄Π°ΠΌΠ΅ ΠΏΠΎ-Π½Π°ΡΠ°ΡΡΠΊ Π² Π³Π»Π°Π²Π°ΡΠ° [](info:global-object)). Π’ΠΎΠ²Π° Π΅ ΠΈΡΡΠΎΡΠΈΡΠ΅ΡΠΊΠΎ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅, ΠΊΠΎΠ΅ΡΠΎ `"use strict"` ΠΎΠΏΡΠ°Π²Ρ. -Usually such call is a programming error. If there's `this` inside a function, it expects to be called in an object context. +ΠΠ±ΠΈΠΊΠ½ΠΎΠ²Π΅Π½ΠΎ ΡΠ°ΠΊΠΎΠ²Π° ΠΈΠ·Π²ΠΈΠΊΠ²Π°Π½Π΅ Π΅ Π³ΡΠ΅ΡΠΊΠ° Π½Π° ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΈΡΡΠ°. ΠΠΊΠΎ ΠΈΠΌΠ° `this` Π²ΡΡΡΠ΅ Π²ΡΠ² ΡΡΠ½ΠΊΡΠΈΡ, ΡΠΎ ΠΎΡΠ°ΠΊΠ²Π° Π΄Π° Π±ΡΠ΄Π΅ ΠΈΠ·Π²ΠΈΠΊΠ°Π½ΠΎ Π² ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° Π½Π° ΠΎΠ±Π΅ΠΊΡ. ```` -```smart header="The consequences of unbound `this`" -If you come from another programming language, then you are probably used to the idea of a "bound `this`", where methods defined in an object always have `this` referencing that object. +```smart header="ΠΠΎΡΠ»Π΅Π΄ΠΈΡΠΈΡΠ΅ ΠΎΡ ΡΠ²ΠΎΠ±ΠΎΠ΄Π΅Π½ `this`" +ΠΠΊΠΎ ΠΈΠ΄Π²Π°ΡΠ΅ ΠΎΡ Π΄ΡΡΠ³ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠ΅Π½ Π΅Π·ΠΈΠΊ Π²Π΅ΡΠΎΡΡΠ½ΠΎ ΡΡΠ΅ Π·Π°ΠΏΠΎΠ·Π½Π°ΡΠΈ Ρ ΠΈΠ΄Π΅ΡΡΠ° Π·Π° "ΠΎΠ±Π²ΡΡΠ·Π°Π½ `this`", ΠΊΡΠ΄Π΅ΡΠΎ ΠΌΠ΅ΡΠΎΠ΄ΠΈΡΠ΅, Π΄Π΅ΡΠΈΠ½ΠΈΡΠ°Π½ΠΈ Π² Π΅Π΄ΠΈΠ½ ΠΎΠ±Π΅ΠΊΡ, Π²ΠΈΠ½Π°Π³ΠΈ ΠΈΠΌΠ°Ρ `this`, ΠΊΠΎΠ΅ΡΠΎ ΡΠ΅ΡΠ΅ΡΠΈΡΠ° ΡΠΎΠ·ΠΈ ΠΎΠ±Π΅ΠΊΡ. -In JavaScript `this` is "free", its value is evaluated at call-time and does not depend on where the method was declared, but rather on what object is "before the dot". +Π JavaScript `this` Π΅ "ΡΠ²ΠΎΠ±ΠΎΠ΄Π΅Π½", Π½Π΅Π³ΠΎΠ²Π°ΡΠ° ΡΡΠΎΠΉΠ½ΠΎΡΡ ΡΠ΅ ΠΈΠ·ΡΠΈΡΠ»ΡΠ²Π° ΠΏΠΎ Π²ΡΠ΅ΠΌΠ΅ Π½Π° ΠΈΠ·Π²ΠΈΠΊΠ²Π°Π½Π΅ ΠΈ Π½Π΅ Π·Π°Π²ΠΈΡΠΈ ΠΎΡ ΡΠΎΠ²Π° ΠΊΡΠ΄Π΅ Π΅ Π΄Π΅ΡΠΈΠ½ΠΈΡΠ°Π½ ΠΌΠ΅ΡΠΎΠ΄Π°, Π° ΠΎΡ ΡΠΎΠ²Π° ΠΊΠΎΠΉ Π΅ ΠΎΠ±Π΅ΠΊΡΠ° "ΠΏΡΠ΅Π΄ΠΈ ΡΠΎΡΠΊΠ°ΡΠ°". -The concept of run-time evaluated `this` has both pluses and minuses. On the one hand, a function can be reused for different objects. On the other hand, the greater flexibility creates more possibilities for mistakes. +ΠΠΎΠ½ΡΠ΅ΠΏΡΠΈΡΡΠ° Π·Π° `this`, ΠΊΠΎΠΉΡΠΎ ΡΠ΅ ΠΈΠ·ΡΠΈΡΠ»ΡΠ²Π° ΠΏΠΎ Π²ΡΠ΅ΠΌΠ΅ Π½Π° ΠΈΠ·ΠΏΡΠ»Π½Π΅Π½ΠΈΠ΅ Π½Π° ΠΊΠΎΠ΄Π° ΠΈΠΌΠ° ΠΊΠ°ΠΊΡΠΎ ΠΏΠ»ΡΡΠΎΠ²Π΅, ΡΠ°ΠΊΠ° ΠΈ ΠΌΠΈΠ½ΡΡΠΈ. ΠΡ Π΅Π΄Π½Π° ΡΡΡΠ°Π½Π° ΡΡΠ½ΠΊΡΠΈΡΡΠ° ΠΌΠΎΠΆΠ΅ Π΄Π° ΡΠ΅ ΠΏΡΠ΅ΠΈΠ·ΠΏΠΎΠ»Π·Π²Π° Π·Π° Π΄ΡΡΠ³ΠΈ ΠΎΠ±Π΅ΠΊΡΠΈ. ΠΡ Π΄ΡΡΠ³Π° ΡΡΡΠ°Π½Π° Π³ΠΎΠ»ΡΠΌΠ°ΡΠ° Π³ΡΠ²ΠΊΠ°Π²ΠΎΡΡ ΡΡΠ·Π΄Π°Π²Π° ΠΏΠΎΠ²Π΅ΡΠ΅ Π²ΡΠ·ΠΌΠΎΠΆΠ½ΠΎΡΡΠΈ Π·Π° Π³ΡΠ΅ΡΠΊΠ°. -Here our position is not to judge whether this language design decision is good or bad. We'll understand how to work with it, how to get benefits and avoid problems. +ΠΠ°ΡΠ°ΡΠ° ΠΏΠΎΠ·ΠΈΡΠΈΡ ΡΡΠΊ Π½Π΅ Π΅ Π΄Π° ΠΎΡΡΡΠ΄ΠΈΠΌ Π΄Π°Π»ΠΈ ΡΠΎΠ·ΠΈ Π΄ΠΈΠ·Π°ΠΉΠ½ Π½Π° Π΅Π·ΠΈΠΊΠ° Π΅ Π΄ΠΎΠ±ΡΡ ΠΈΠ»ΠΈ Π»ΠΎΡ. ΠΠΈΠ΅ ΡΠ΅ ΠΎΠ±ΡΡΠ½ΠΈΠΌ ΠΊΠ°ΠΊ Π΄Π° ΡΠ°Π±ΠΎΡΠΈΡΠ΅ Ρ Π½Π΅Π³ΠΎ, ΠΊΠ°ΠΊ Π΄Π° ΠΈΠ·Π²Π»ΠΈΡΠ°ΡΠ΅ ΠΏΠΎΠ»Π·ΠΈΡΠ΅ ΠΈ Π΄Π° ΠΈΠ·Π±ΡΠ³Π²Π°ΡΠ΅ ΠΏΡΠΎΠ±Π»Π΅ΠΌΠΈ. ``` -## Arrow functions have no "this" +## Arrow ΡΡΠ½ΠΊΡΠΈΠΈΡΠ΅ Π½ΡΠΌΠ°Ρ "this" -Arrow functions are special: they don't have their "own" `this`. If we reference `this` from such a function, it's taken from the outer "normal" function. +Arrow ΡΡΠ½ΠΊΡΠΈΠΈΡΠ΅ ΡΠ° ΡΠΏΠ΅ΡΠΈΠ°Π»Π½ΠΈ: ΡΠ΅ Π½ΡΠΌΠ°Ρ "ΡΠΎΠ±ΡΡΠ²Π΅Π½" `this`. ΠΠΊΠΎ ΡΠ΅ΡΠ΅ΡΠΈΡΠ°ΠΌΠ΅ `this` ΠΎΡ ΡΠ°ΠΊΠ°Π²Π° ΡΡΠ½ΠΊΡΠΈΡ, ΡΠΎΠΉ ΡΠ΅ Π²Π·ΠΈΠΌΠ° ΠΎΡ Π²ΡΠ½ΡΠ½Π°ΡΠ° "Π½ΠΎΡΠΌΠ°Π»Π½Π°" ΡΡΠ½ΠΊΡΠΈΡ. -For instance, here `arrow()` uses `this` from the outer `user.sayHi()` method: +ΠΠ°ΠΏΡΠΈΠΌΠ΅Ρ, ΡΡΠΊ `arrow()` ΠΈΠ·ΠΏΠΎΠ»Π·Π²Π° `this` ΠΎΡ Π²ΡΠ½ΡΠ½ΠΈΡΡ `user.sayHi()` ΠΌΠ΅ΡΠΎΠ΄: ```js run let user = { @@ -251,18 +251,18 @@ let user = { user.sayHi(); // Ilya ``` -That's a special feature of arrow functions, it's useful when we actually do not want to have a separate `this`, but rather to take it from the outer context. Later in the chapterNote: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: