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/09-classes/07-mixins/article.md
+17-17Lines changed: 17 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,11 +12,11 @@
12
12
13
13
به عبارتی دیگر، یک *mixin* متدهایی که یک کار مشخص انجام میدهند را فراهم میکند اما از آن به تنهایی استفاده نمیکنیم بلکه از آن برای اضافه کردن همان کار مشخص به کلاسهای دیگر استفاده میکنیم.
14
14
15
-
## A mixin example
15
+
## یک مثال mixin
16
16
17
-
The simplest way to implement a mixin in JavaScript is to make an object with useful methods, so that we can easily merge them into a prototype of any class.
17
+
سادهترین راه برای پیادهسازی یک mixin در جاوااسکریپت ایجاد شیءای شامل متدهایی مفید است تا بتوانیم به راحتی آنها را درون پروتوتایپ هر کلاسی ادغام کنیم.
18
18
19
-
For instance here the mixin `sayHiMixin`is used to add some "speech" for `User`:
19
+
برای مثال اینجا `sayHiMixin`برای اضافه کردن «گفتار» به `User` استفاده شده است:
20
20
21
21
```js run
22
22
*!*
@@ -32,22 +32,22 @@ let sayHiMixin = {
32
32
};
33
33
34
34
*!*
35
-
//usage:
35
+
//:کاربرد
36
36
*/!*
37
37
classUser {
38
38
constructor(name) {
39
39
this.name= name;
40
40
}
41
41
}
42
42
43
-
//copy the methods
43
+
//کپی کردن متدها
44
44
Object.assign(User.prototype, sayHiMixin);
45
45
46
-
//now User can say hi
46
+
//بگوید (hi) میتواند سلام User حالا
47
47
newUser("Dude").sayHi(); // Hello Dude!
48
48
```
49
49
50
-
There's no inheritance, but a simple method copying. So`User`may inherit from another class and also include the mixin to "mix-in" the additional methods, like this:
50
+
ارثبری در کار نیست، فقط یک کپی کردن متد ساده است. پس`User`میتواند از کلاس دیگری ارثبری کند و همچنین mixin را هم شامل شود تا متدهای اضافی را «ترکیب» کند، مثل این:
51
51
52
52
```js
53
53
classUserextendsPerson {
@@ -57,9 +57,9 @@ class User extends Person {
57
57
Object.assign(User.prototype, sayHiMixin);
58
58
```
59
59
60
-
Mixins can make use of inheritance inside themselves.
60
+
mixinها میتوانند درون خود از ارثبری استفاده کنند.
61
61
62
-
For instance, here`sayHiMixin`inherits from `sayMixin`:
62
+
برای مثال، اینجا`sayHiMixin`از `sayMixin` ارثبری میکند:
63
63
64
64
```js run
65
65
let sayMixin = {
@@ -69,11 +69,11 @@ let sayMixin = {
69
69
};
70
70
71
71
let sayHiMixin = {
72
-
__proto__: sayMixin, // (or we could use Object.setPrototypeOf to set the prototype here)
72
+
__proto__: sayMixin, // (برای تنظیم پروتوتایپ استفاده کنیم `Object.setPrototypeOf` یا میتوانستیم اینجا از)
73
73
74
74
sayHi() {
75
75
*!*
76
-
//call parent method
76
+
//فراخوانی متد والد
77
77
*/!*
78
78
super.say(`Hello ${this.name}`); // (*)
79
79
},
@@ -88,22 +88,22 @@ class User {
88
88
}
89
89
}
90
90
91
-
//copy the methods
91
+
//کپی کردن متدها
92
92
Object.assign(User.prototype, sayHiMixin);
93
93
94
-
//now User can say hi
94
+
//بگوید (hi) میتواند سلام User حالا
95
95
newUser("Dude").sayHi(); // Hello Dude!
96
96
```
97
97
98
-
Please note that the call to the parent method `super.say()`from`sayHiMixin` (at lines labelled with`(*)`) looks for the method in the prototype of that mixin, not the class.
98
+
لطفا توجه کنید که فراخوانی متد `super.say()`از`sayHiMixin` (در خطی که با`(*)` برچسبگذاری شده) در پروتوتایپ mixin به دنبال متد میگردد نه کلاس.
99
99
100
-
Here's the diagram (see the right part):
100
+
اینجا شکل آن را داریم (قسمت راست را ببینید):
101
101
102
102

103
103
104
-
That's because methods `sayHi`and`sayBye`were initially created in `sayHiMixin`. So even though they got copied, their `[[HomeObject]]`internal property references `sayHiMixin`, as shown in the picture above.
104
+
دلیلش این است که `sayHi`و`sayBye`از اول درون `sayHiMixin` ایجاد شدهاند. پس حتی با اینکه کپی شدند، ویژگی درونی `[[HomeObject]]`آنها به `sayHiMixin` رجوع میکند، همانطور که در تصویر بالا نشان داده شده است.
105
105
106
-
As`super`looks for parent methods in `[[HomeObject]].[[Prototype]]`, that means it searches `sayHiMixin.[[Prototype]]`, not `User.[[Prototype]]`.
106
+
چون`super`درون `[[HomeObject]].[[Prototype]]` به دنبال متدهای والد میگردد، یعنی `sayHiMixin.[[Prototype]]` را جستوجو میکند نه `User.[[Prototype]]` را.
0 commit comments