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
در مقابل، `Symbol.toPrimitice` *باید* مقدار اصلی برگرداند، در غیر این صورت یک ارور خواهیم داشت.
218
218
```
219
219
220
-
## Further conversions
220
+
## تبدیلهای بیشتر
221
221
222
-
As we know already, many operators and functions perform type conversions, e.g. multiplication `*`converts operands to numbers.
222
+
همانطور که از قبل میدانیم، بسیاری از عملگرها و تابعها تبدیل نوع داده را انجام میدهند، مثلا عملگر ضرب `*`عملوندها را به عدد تبدیل میکند.
223
223
224
-
If we pass an object as an argument, then there are two stages:
225
-
1.The object is converted to a primitive (using the rules described above).
226
-
2.If the resulting primitive isn't of the right type, it's converted.
224
+
اگر ما به عنوان آرگومان شیءای را پاس دهیم، دو مرحله وجود خواهد داشت:
225
+
1.شیء به یک مقدار اصلی تبدیل میشود (با استفاده از قوانینی که بالاتر گفتیم).
226
+
2.اگر مقدار اصلی حاصل، نوع درستی نباشد، دوباره تبدیل میشود.
227
227
228
-
For instance:
228
+
برای مثال:
229
229
230
230
```js run
231
231
let obj = {
232
-
//toString handles all conversions in the absence of other methods
232
+
//در نبود بقیه متدها تمام تبدیلها را به عهده میگیرد toString
233
233
toString() {
234
234
return"2";
235
235
}
236
236
};
237
237
238
-
alert(obj *2); //4, object converted to primitive "2", then multiplication made it a number
238
+
alert(obj *2); //شیء به مقدار اصلی "2" تبدیل شد، عملگر ضرب آن را به عدد تبدیل کرد، 4
239
239
```
240
240
241
-
1.The multiplication `obj * 2`first converts the object to primitive (that's a string`"2"`).
242
-
2.Then`"2" * 2`becomes `2 * 2` (the string is converted to number).
241
+
1.ضرب `obj * 2`ابتدا شیء را به مقدار اصلی تبدیل میکند (برابر است با رشته`"2"`).
242
+
2.سپس`"2" * 2`تبدیل میشود به `2 * 2` (رشته به عدد تبدیل میشود).
243
243
244
-
Binary plus will concatenate strings in the same situation, as it gladly accepts a string:
244
+
عملگر مثبت دوگانه همانطور که با خوشحالی یک رشته را قبول میکند، رشتهها را در موقعیت یکسان ادغام میکند:
245
245
246
246
```js run
247
247
let obj = {
@@ -250,28 +250,28 @@ let obj = {
250
250
}
251
251
};
252
252
253
-
alert(obj +2); //22 ("2" + 2), conversion to primitive returned a string => concatenation
253
+
alert(obj +2); //تبدیل به مقدار اصلی، یک رشته => ادغام برگرداند، (2 + "2") 22
254
254
```
255
255
256
-
## Summary
256
+
## خلاصه
257
257
258
-
The object-to-primitive conversion is called automatically by many built-in functions and operators that expect a primitive as a value.
258
+
تبدیل شیء به مقدار اصلی توسط بسیاری از تابعها و عملگرهای درونساخت که مقداری اصلی را به عنوان ورودی قبول میکنند به طور خودکار فراخوانی میشود.
259
259
260
-
There are 3 types (hints) of it:
261
-
-`"string"` (for`alert`and other operations that need a string)
262
-
-`"number"` (for maths)
263
-
-`"default"` (few operators)
260
+
سه نوع (جزء) از آن وجود دارد:
261
+
-`"string"` (برای`alert`و عملیات دیگر که به رشته نیاز دارند)
262
+
-`"number"` (برای ریاضیات)
263
+
-`"default"` (تعداد کمی عملگر)
264
264
265
-
The specification describes explicitly which operator uses which hint. There are very few operators that "don't know what to expect" and use the `"default"`hint. Usually for built-in objects`"default"`hint is handled the same way as `"number"`, so in practice the last two are often merged together.
265
+
مشخصات زبان به طور واضح بیان کرده است که کدام عملگر از کدام جزء استفاده میکند. تعداد کمی عملگر وجود دارد که «نمیدانند توقع چه چیزی را داشته باشند» و از جزء `"default"`استفاده میکنند. معمولا برای شیءهای درونساخت،`"default"`درست مانند `"number"` استفاده میشود، پس در عمل این دو اغلب اوقات با هم ادغام میشوند.
266
266
267
-
The conversion algorithm is:
267
+
الگوریتم تبدیل:
268
268
269
-
1.Call`obj[Symbol.toPrimitive](hint)`if the method exists,
1.فراخوانی`obj[Symbol.toPrimitive](hint)`در صورتی که وجود داشت،
270
+
2.در غیر این صورت اگر جزء `"string"` بود
271
+
-متدهای`obj.toString()`و`obj.valueOf()` را امتحان کن، هر کدام که وجود داشت.
272
+
3.در غیر این صورت اگر جزء `"number"`یا`"default"` بود
273
+
-متدهای`obj.valueOf()`و`obj.toString()` را امتحان کن، هر کدام که وجود داشت.
274
274
275
-
In practice, it's often enough to implement only `obj.toString()`as a "catch-all" method for string conversions that should return a "human-readable" representation of an object, for logging or debugging purposes.
275
+
در عمل، اینکه فقط `obj.toString()`را به عنوان متدی «همهگیر» برای تبدیلهای رشتهای که باید یک نمایش «قابل خواندن برای انسان» از شیء را برگدانند، برای اهداف دیباگ یا لاگ گرفتن، اغلب اوقات کافی است.
276
276
277
-
As for math operations, JavaScript doesn't provide a way to "override" them using methods, so real life projects rarely use them on objects.
277
+
همینطور برای عملیات ریاضی، جاوااسکریپت راهی برای باطل کردن آنها با استفاده از متدها را فراهم نمیکند، پس پروژههای زندگی واقعی به ندرت از آنها روی شیءها استفاده میکنند.
0 commit comments