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/04-private-protected-properties-methods/article.md
+15-15Lines changed: 15 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -59,37 +59,37 @@
59
59
60
60
حالا در جاوااسکریپت یک قهوهساز همراه با انواع ویژگی خواهیم ساخت. یک قهوهساز جزئیات زیادی دارد، ما برای ساده بودن آنها را مدلسازی نمیکنیم (اگرچه میتوانستیم).
61
61
62
-
## Protecting "waterAmount"
62
+
## ویژگی "waterAmount" محافظتشده
63
63
64
-
Let's make a simple coffee machine class first:
64
+
بیایید یک کلاس ساده قهوهساز ایجاد کنیم:
65
65
66
66
```js run
67
67
classCoffeeMachine {
68
-
waterAmount =0; //the amount of water inside
68
+
waterAmount =0; //مقدار آب درون
69
69
70
70
constructor(power) {
71
71
this.power= power;
72
-
alert( `Created a coffee-machine, power: ${power}` );
72
+
alert( `یک قهوهساز ایجاد کردیم، قدرت: ${power}` );
73
73
}
74
74
75
75
}
76
76
77
-
//create the coffee machine
77
+
//ایجاد قهوهساز
78
78
let coffeeMachine =newCoffeeMachine(100);
79
79
80
-
//add water
80
+
//اضافه کردن آب
81
81
coffeeMachine.waterAmount=200;
82
82
```
83
83
84
-
Right now the properties `waterAmount`and`power`are public. We can easily get/set them from the outside to any value.
84
+
حالا ویژگیهای `waterAmount`و`power`عمومی هستند. میتوانیم به راحتی از بیرون آنها را دریافت کنیم یا مقداردهی کنیم.
85
85
86
-
Let's change `waterAmount` property to protected to have more control over it. For instance, we don't want anyone to set it below zero.
86
+
بیایید برای داشتن کنترل بیشتر ویژگی `waterAmount` را به محافظتشده تغییر دهیم. برای مثال، ما نمیخواهیم کسی آن را کمتر از صفر تنظیم کند.
87
87
88
-
**Protected properties are usually prefixed with an underscore `_`.**
88
+
**قبل از ویژگیهای محافظتشده معمولا یک زیرخط (underscore)`_` میآید.**
89
89
90
-
That is not enforced on the language level, but there's a well-known convention between programmers that such properties and methods should not be accessed from the outside.
90
+
این نوع در سطح زبان اجرایی نشده اما یک قرارداد شناختهشده بین برنامهنویسان وجود دارد که نباید از بیرون به چنین ویژگیها و متدهایی دسترسی پیدا کرد.
91
91
92
-
So our property will be called `_waterAmount`:
92
+
پس ویژگی ما `_waterAmount` خواهد بود:
93
93
94
94
```js run
95
95
classCoffeeMachine {
@@ -112,14 +112,14 @@ class CoffeeMachine {
112
112
113
113
}
114
114
115
-
//create the coffee machine
115
+
//ایجاد قهوهساز
116
116
let coffeeMachine =newCoffeeMachine(100);
117
117
118
-
//add water
119
-
coffeeMachine.waterAmount=-10; //_waterAmount will become 0, not -10
118
+
//اضافه کردن آب
119
+
coffeeMachine.waterAmount=-10; //-برابر با 0 خواهد بود نه 10 _waterAmount
120
120
```
121
121
122
-
Now the access is under control, so setting the water amount below zero becomes impossible.
122
+
حالا دسترسی تحت کنترل است پس تنظیم مقدار آب کمتر از صفر ممکن نیست.
0 commit comments