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
+18-18Lines changed: 18 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -188,11 +188,11 @@ new CoffeeMachine().setWaterAmount(100);
188
188
189
189
[recent browser=none]
190
190
191
-
There's a finished JavaScript proposal, almost in the standard, that provides language-level support for private properties and methods.
191
+
یک طرح پیشنهادی تمام شده جاوااسکریپت وجود دارد، تقریبا درون استاندارد وارد شده، که پشتیبانی برای ویژگیها و متدهای خصوصی (private) را در سطح زبان فراهم میکند.
192
192
193
-
Privates should start with `#`. They are only accessible from inside the class.
193
+
ویژگیها خصوصی با `#` شروع میشوند. آنها فقط از درون کلاس قابل دسترس هستند.
194
194
195
-
For instance, here's a private`#waterLimit`property and the water-checking private method `#fixWaterAmount`:
195
+
برای مثال، اینجا ویژگی خصوصی`#waterLimit`و متد خصوصی `#fixWaterAmount` را داریم:
196
196
197
197
```js run
198
198
classCoffeeMachine {
@@ -216,17 +216,17 @@ class CoffeeMachine {
216
216
let coffeeMachine =newCoffeeMachine();
217
217
218
218
*!*
219
-
//can't access privates from outside of the class
220
-
coffeeMachine.#fixWaterAmount(123); //Error
221
-
coffeeMachine.#waterLimit =1000; //Error
219
+
//نمیتوان بیرون از کلاس به خصوصیها دسترسی پیدا کرد
220
+
coffeeMachine.#fixWaterAmount(123); //ارور
221
+
coffeeMachine.#waterLimit =1000; //ارور
222
222
*/!*
223
223
```
224
224
225
-
On the language level, `#`is a special sign that the field is private. We can't access it from outside or from inheriting classes.
225
+
در سطح زبان، `#`نمادی خاص است که یعنی فیلد خصوصی است. ما نمیتوانیم از بیرون یا از کلاسهای ارثبر به آن دسترسی داشته باشیم.
226
226
227
-
Private fields do not conflict with public ones. We can have both private`#waterAmount`and public `waterAmount`fields at the same time.
227
+
فیلدهای خصوصی با فیلدهای عمومی ناسازگار نیستند. میتوانیم در یک زمان هم`#waterAmount`خصوصی داشته باشیم و هم `waterAmount`عمومی.
228
228
229
-
For instance, let's make `waterAmount`an accessor for `#waterAmount`:
229
+
برای مثال، بیایید `waterAmount`را به عنوان اکسسر برای `#waterAmount` ایجاد کنیم:
230
230
231
231
```js run
232
232
classCoffeeMachine {
@@ -246,29 +246,29 @@ class CoffeeMachine {
246
246
let machine =newCoffeeMachine();
247
247
248
248
machine.waterAmount=100;
249
-
alert(machine.#waterAmount); //Error
249
+
alert(machine.#waterAmount); //ارور
250
250
```
251
251
252
-
Unlike protected ones, private fields are enforced by the language itself. That's a good thing.
252
+
برخلاف محافظتشدهها، فیلدهای خصوصی در سطح خود زبان اجرایی شدهاند. این چیز خوبی است.
253
253
254
-
But if we inherit from `CoffeeMachine`, then we'll have no direct access to `#waterAmount`. We'll need to rely on`waterAmount`getter/setter:
254
+
اما اگر ما از `CoffeMachine` ارثبری کنیم، سپس دسترسی مستقیم به `#waterAmount` نداریم. باید به سراغ getter/setter برای`waterAmount`برویم:
255
255
256
256
```js
257
257
classMegaCoffeeMachineextendsCoffeeMachine {
258
258
method() {
259
259
*!*
260
-
alert( this.#waterAmount ); //Error: can only access from CoffeeMachine
260
+
alert( this.#waterAmount ); //ممکن است CoffeMachine ارور: دسترسی فقط از
261
261
*/!*
262
262
}
263
263
}
264
264
```
265
265
266
-
In many scenarios such limitation is too severe. If we extend a `CoffeeMachine`, we may have legitimate reasons to access its internals. That's why protected fields are used more often, even though they are not supported by the language syntax.
266
+
در بسیاری از سناریوها چنین محدودیتی خیلی سختگیرانه است. اگر ما `CoffeMachine` را تعمیم دهیم، شاید دلایل قابل قبولی برای دسترسی به درون آن داشته باشیم. به همین دلیل فیلدهای محافظتشده اغلب اوقات استفاده میشوند حتی اگر آنها توسط سینتکس زبان پشتیبانی نمیشوند.
267
267
268
-
````warn header="Private fields are not available as this[name]"
269
-
Private fields are special.
268
+
````warn header="فیلدهای خصوصی به صورت this[name] در دسترس نیستند"
269
+
فیلدهای خصوصی خاص هستند.
270
270
271
-
As we know, usually we can access fields using `this[name]`:
271
+
همانطور که میدانیم، معمولا با استفاده از `this[name]` به فیلدها دسترسی پیدا میکنیم:
272
272
273
273
```js
274
274
class User {
@@ -280,7 +280,7 @@ class User {
280
280
}
281
281
```
282
282
283
-
With private fields that's impossible: `this['#name']` doesn't work. That's a syntax limitation to ensure privacy.
283
+
این برای فیلدهای خصوصی غیر ممکن است: `this['#name']` کار نمیکند. این یک محدودیت سینتکسی است تا حریم اطمینان حاصل کند.
0 commit comments