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/08-prototypes/01-prototype-inheritance/4-hamster-proto/solution.md
+22-22Lines changed: 22 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,26 @@
1
-
Let's look carefully at what's going on in the call `speedy.eat("apple")`.
1
+
بیایید با دقت نگاه کنیم که در فراخوانی `speedy.eat("سیب")` چه اتفاقی میافتد.
2
2
3
-
1.The method `speedy.eat`is found in the prototype (`=hamster`), then executed with `this=speedy`(the object before the dot).
3
+
1.متد `speedy.eat`درون پروتوتایپ (`=hamster`) پیدا شده، سپس با `this=speedy`اجرا میشود (شیء قبل از نقطه).
4
4
5
-
2.Then`this.stomach.push()`needs to find `stomach`property and call `push`on it. It looks for `stomach` in `this` (`=speedy`), but nothing found.
5
+
2.سپس`this.stomach.push()`باید ویژگی `stomach`را پیدا کند و `push`را روی آن فراخوانی کند. به نظر میرسد این متد درون `this` (`=speedy`) به دنبال `stomach` میگردد، اما چیزی پیدا نشد.
6
6
7
-
3.Then it follows the prototype chain and finds `stomach`in `hamster`.
7
+
3.سپس زنجیره پروتوتایپ را دنبال میکند و `stomach`را درون `hamster` پیدا میکند.
8
8
9
-
4.Then it calls `push`on it, adding the food into *the stomach of the prototype*.
9
+
4.سپس `push`را روی آن فراخوانی میکند، که غذا را به *stomach درون پروتوتایپ* اضافه میکند.
10
10
11
-
So all hamsters share a single stomach!
11
+
پس تمام همسترها شکم (stomach) یکسانی را به اشتراک میگذارند!
12
12
13
-
Both for`lazy.stomach.push(...)`and`speedy.stomach.push()`, the property `stomach`is found in the prototype (as it's not in the object itself), then the new data is pushed into it.
13
+
هم برای`lazy.stomach.push(...)`و`speedy.stomach.push()`، ویژگی `stomach`درون پروتوتایپ پیدا شده است (چون درون خود شیء وجود ندارد)، سپس داده جدید به داخل آن فرستاده میشود.
14
14
15
-
Please note that such thing doesn't happen in case of a simple assignment `this.stomach=`:
15
+
لطفا توجه کنید که در صورت وجود یک مقداردهی ساده `this.stomach=` چنین چیزی اتفاق نمیافتد:
16
16
17
17
```js run
18
18
let hamster = {
19
19
stomach: [],
20
20
21
21
eat(food) {
22
22
*!*
23
-
//assign to this.stomach instead of this.stomach.push
23
+
// this.stomach.push به جای this.stomach برابر قرار دادن با
24
24
this.stomach= [food];
25
25
*/!*
26
26
}
@@ -34,17 +34,17 @@ let lazy = {
34
34
__proto__: hamster
35
35
};
36
36
37
-
//Speedy one found the food
38
-
speedy.eat("apple");
39
-
alert( speedy.stomach ); //apple
37
+
//غذا را پیدا کرد Speedy همستر
38
+
speedy.eat("سیب");
39
+
alert( speedy.stomach ); //سیب
40
40
41
-
//Lazy one's stomach is empty
42
-
alert( lazy.stomach ); // <nothing>
41
+
//خالی است Lazy شکم همستر
42
+
alert( lazy.stomach ); // <هیچی>
43
43
```
44
44
45
-
Now all works fine, because `this.stomach=`does not perform a lookup of `stomach`. The value is written directly into`this`object.
45
+
حالا همه چیز به درستی کار میکند، چون `this.stomach=`در جست و جوی `stomach` نیست. مقدار به صورت مستقیم درون شیء`this`نوشته میشود.
46
46
47
-
Also we can totally avoid the problem by making sure that each hamster has their own stomach:
47
+
همچنین میتوانیم با اطمینان از اینکه هر همستر stomach خودش را دارا میباشد از این مشکل جلوگیری کنیم:
48
48
49
49
```js run
50
50
let hamster = {
@@ -69,12 +69,12 @@ let lazy = {
69
69
*/!*
70
70
};
71
71
72
-
//Speedy one found the food
73
-
speedy.eat("apple");
74
-
alert( speedy.stomach ); //apple
72
+
//غذا را پیدا کرد Speedy همستر
73
+
speedy.eat("سیب");
74
+
alert( speedy.stomach ); //سیب
75
75
76
-
//Lazy one's stomach is empty
77
-
alert( lazy.stomach ); // <nothing>
76
+
//خالی است Lazy شکم همستر
77
+
alert( lazy.stomach ); // <هیچی>
78
78
```
79
79
80
-
As a common solution, all properties that describe the state of a particular object, like `stomach`above, should be written into that object. That prevents such problems.
80
+
به عنوان یک راهحل عام، تمام ویژگیهایی که وضعیت یک شیء خاص را توصیف میکنند، مانند `stomach`بالا، باید درون همان شیء نوشته شوند. این کار از بروز مشکلات جلوگیری میکند.
0 commit comments