pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

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 chapter we'll go more deeply into arrow functions. +Π’ΠΎΠ²Π° Π΅ спСциално свойство Π½Π° arrow Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈΡ‚Π΅, ΠΈΠ·ΠΏΠΎΠ»Π·Π²Π° сС ΠΊΠΎΠ³Π°Ρ‚ΠΎ Π½Π΅ искамС Π΄Π° ΠΈΠΌΠ°ΠΌΠ΅ ΠΎΡ‚Π΄Π΅Π»Π΅Π½ `this`, Π° вмСсто Ρ‚ΠΎΠ²Π° Π΄Π° Π³ΠΎ Π²Π·Π΅ΠΌΠ΅ΠΌ ΠΎΡ‚ външния коитСкст. По-късно Π² Π³Π»Π°Π²Π°Ρ‚Π° Ρ‰Π΅ Ρ€Π°Π·Π³Π»Π΅Π΄Π°ΠΌΠ΅ ΠΏΠΎΠ΄Ρ€ΠΎΠ±Π½ΠΎ arrow Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈΡ‚Π΅. -## Summary +## ΠžΠ±ΠΎΠ±Ρ‰Π΅Π½ΠΈΠ΅ -- Functions that are stored in object properties are called "methods". -- Methods allow objects to "act" like `object.doSomething()`. -- Methods can reference the object as `this`. +- Π€ΡƒΠ½ΠΊΡ†ΠΈΠΈ, ΠΊΠΎΠΈΡ‚ΠΎ сС Π½Π°ΠΌΠΈΡ€Π°Ρ‚ Π² ΠΏΡ€ΠΎΠΏΡŠΡ€Ρ‚ΠΈΡ‚Π° Π½Π° ΠΎΠ±Π΅ΠΊΡ‚ΠΈ, сС Π½Π°Ρ€ΠΈΡ‡Π°Ρ‚ "ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΈ". +- ΠœΠ΅Ρ‚ΠΎΠ΄ΠΈΡ‚Π΅ позволяват Π½Π° ΠΎΠ±Π΅ΠΊΡ‚ΠΈΡ‚Π΅ Π΄Π° "ΠΈΠ·Π²ΡŠΡ€ΡˆΠ²Π°Ρ‚ дСйствия" ΠΊΠ°Ρ‚ΠΎ `object.doSomething()`. +- ΠœΠ΅Ρ‚ΠΎΠ΄ΠΈΡ‚Π΅ ΠΌΠΎΠ³Π°Ρ‚ Π΄Π° Ρ€Π΅Ρ„Π΅Ρ€ΠΈΡ€Π°Ρ‚ ΠΎΠ±Π΅ΠΊΡ‚Π° с ΠΊΠ»ΡŽΡ‡ΠΎΠ²Π°Ρ‚Π° Π΄ΡƒΠΌΠ° `this`. -The value of `this` is defined at run-time. -- When a function is declared, it may use `this`, but that `this` has no value until the function is called. -- A function can be copied between objects. -- When a function is called in the "method" syntax: `object.method()`, the value of `this` during the call is `object`. +Бтойността Π½Π° `this` сС опрСдСля ΠΏΠΎ Π²Ρ€Π΅ΠΌΠ΅ Π½Π° ΠΈΠ·ΠΏΡŠΠ»Π½Π΅Π½ΠΈΠ΅Ρ‚ΠΎ Π½Π° ΠΊΠΎΠ΄Π°. +- ΠšΠΎΠ³Π°Ρ‚ΠΎ Π΄Π΅ΠΊΠ»Π°Ρ€ΠΈΡ€Π°ΠΌΠ΅ функция, тя ΠΌΠΎΠΆΠ΅ Π΄Π° ΠΈΠ·ΠΏΠΎΠ»Π·Π²Π° `this`, Π½ΠΎ Ρ‚ΠΎΠ²Π° `this` няма Π΄Π° ΠΈΠΌΠ° стойност Π΄ΠΎΠΊΠ°Ρ‚ΠΎ Π½Π΅ ΠΈΠ·Π²ΠΈΠΊΠ°ΠΌΠ΅ функцията. +- Π•Π΄Π½Π° функция ΠΌΠΎΠΆΠ΅ Π΄Π° сС ΠΊΠΎΠΏΠΈΡ€Π° ΠΌΠ΅ΠΆΠ΄Ρƒ ΠΎΠ±Π΅ΠΊΡ‚ΠΈ. +- ΠšΠΎΠ³Π°Ρ‚ΠΎ функция Π΅ ΠΈΠ·Π²ΠΈΠΊΠ°Π½Π° Ρ‡Ρ€Π΅Π· слСдния "ΠΌΠ΅Ρ‚ΠΎΠ΄" синтаксис: `object.method()`, стойността Π½Π° `this` ΠΏΠΎ Π²Ρ€Π΅ΠΌΠ΅ Π½Π° ΠΈΠ·Π²ΠΈΠΊΠ²Π°Π½Π΅Ρ‚ΠΎ Π΅ `ΠΎΠ±Π΅ΠΊΡ‚`. -Please note that arrow functions are special: they have no `this`. When `this` is accessed inside an arrow function, it is taken from outside. +Π˜ΠΌΠ°ΠΉΡ‚Π΅ ΠΏΡ€Π΅Π΄Π²ΠΈΠ΄, Ρ‡Π΅ arrow Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈΡ‚Π΅ са спСциални: Ρ‚Π΅ нямат `this`. ΠšΠΎΠ³Π°Ρ‚ΠΎ ΠΈΠ·ΠΏΠΎΠ»Π·Π²Π°ΠΌΠ΅ `this` Π² arrow функция, Π½Π΅Π³ΠΎΠ²Π°Ρ‚Π° стойност сС Π²Π·ΠΈΠΌΠ° ΠΎΡ‚Π²ΡŠΠ½. pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: 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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy