Skip to content

Commit d34d0a7

Browse files
authored
Merge pull request #303 from nafasebra/master
Translate 'Reference Type' session #1
2 parents 837125b + 65b6954 commit d34d0a7

File tree

5 files changed

+62
-63
lines changed

5 files changed

+62
-63
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
**Error**!
1+
**خطا**!
22

3-
Try it:
3+
کد زیر را مدنظر داشته باشید:
44

55
```js run
66
let user = {
77
name: "John",
88
go: function() { alert(this.name) }
99
}
1010

11-
(user.go)() // error!
11+
(user.go)() // خطا دارد!
1212
```
1313

14-
The error message in most browsers does not give us much of a clue about what went wrong.
14+
پیام خطا در بیشتر مرورگر ها اطلاعاتی بابت اینکه چه اشتباهی رخ داده است نمیدهد.
1515

16-
**The error appears because a semicolon is missing after `user = {...}`.**
16+
** این خطا نشان داده میشود چون سمیکالن بعد از این دستور فراموش شده است `{...} = user`.**
1717

18-
JavaScript does not auto-insert a semicolon before a bracket `(user.go)()`, so it reads the code like:
18+
جاوااسکریپت به صورت پیش فرض سمیکالن را در براکت قبل از دستور `()(user.go)` اضافه نمیکند. سپس کد به این صورت خوانده میشود:
1919

2020
```js no-beautify
2121
let user = { go:... }(user.go)()
2222
```
2323

24-
Then we can also see that such a joint expression is syntactically a call of the object `{ go: ... }` as a function with the argument `(user.go)`. And that also happens on the same line with `let user`, so the `user` object has not yet even been defined, hence the error.
24+
بنابراین جاوااسکریپت حدس میزند که ما درواقع آبجکت `{ ... :go }` را به عنوان یک تابع با آرگومان های `(user.go)` صدا زده ایم. و سپس چون به `let user` میرسیم آبجکت `user` به صورت تعریف نشده معرفی میشود و ما ارور را مشاهده میکنیم.
2525

26-
If we insert the semicolon, all is fine:
26+
در کد زیر همه چیز خوب بنظر میرسد اگر سمیکالن را انتهای آبجکت اضافه کنیم:
2727

2828
```js run
2929
let user = {
@@ -34,4 +34,4 @@ let user = {
3434
(user.go)() // John
3535
```
3636

37-
Please note that parentheses around `(user.go)` do nothing here. Usually they setup the order of operations, but here the dot `.` works first anyway, so there's no effect. Only the semicolon thing matters.
37+
توجه داشته باشید که پرانتز اطراف `(user.go)` کاری انجام نمیدهد. معمولا در اکثر موقعیت ها اولویت پرانتز عملگر هارا تغییر میدهد, اما در اینجا فقط عملگر نقطه `.` کار میکند. سپس مورد خاصی نیست و فعلا بحث سمیکالن مهم است.

1-js/99-js-misc/04-reference-type/2-check-syntax/task.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 2
22

33
---
44

5-
# Syntax check
5+
# بررسی سینتکس
66

7-
What is the result of this code?
7+
خروجی کد زیر چیست؟
88

99

1010
```js no-beautify
@@ -16,4 +16,4 @@ let user = {
1616
(user.go)()
1717
```
1818

19-
P.S. There's a pitfall :)
19+
نکته: سوال دام دار است (یعنی نکته دارد) :)
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11

2-
Here's the explanations.
2+
با توجه به موارد زیر.
33

4-
1. That's a regular object method call.
4+
1. متدی که داخل یک آبجکت معمولی بوده, اجرا شده.
55

6-
2. The same, parentheses do not change the order of operations here, the dot is first anyway.
6+
2. به همین ترتیب, پرانتز در اینجا اولیت را عوض نمیکند و مهم نقطه `.` است.
77

8-
3. Here we have a more complex call `(expression)()`. The call works as if it were split into two lines:
8+
3. ما نمونه پیچیده تری نسبت به `()(expression)`. در زیر داریم که به دو خط کد تقسیم شده است:
99

1010
```js no-beautify
1111
f = obj.go; // calculate the expression
1212
f(); // call what we have
1313
```
1414

15-
Here `f()` is executed as a function, without `this`.
15+
اینجا `()f` به عنوان یک تابع, بدون `this` اجرا میشود.
1616

17-
4. The similar thing as `(3)`, to the left of the parentheses `()` we have an expression.
17+
4. در موارد مشابه `(3)`, در سمت چپ پرانتز عبارت داریم.
1818

19-
To explain the behavior of `(3)` and `(4)` we need to recall that property accessors (dot or square brackets) return a value of the Reference Type.
20-
21-
Any operation on it except a method call (like assignment `=` or `||`) turns it into an ordinary value, which does not carry the information allowing to set `this`.
19+
برای اینکه اتفاقاتی که در مورد `(3)` و `(4)` رج میدهد را بدانیم, باید به خاطر داشته باشیم که دسترسی به مقدار ویژگی ها (چه با نقطه یا چه با استفاده از براکت) یک مقداری از Reference Type
2220

21+
هر عملیاتی که برروی آن غیر از توابع (مثل عملگر تخصیص `=` یا `||`) اعمال میشود, از آن یک مقدار معمولی میسازد, که اطلاعاتی برای تنظیم مقدار `this` ندارد.

1-js/99-js-misc/04-reference-type/3-why-this/task.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 3
22

33
---
44

5-
# Explain the value of "this"
5+
# مقدار "this" را تشریح کنید.
66

7-
In the code below we intend to call `obj.go()` method 4 times in a row.
7+
در کد زیر قصد داریم متد `()obj.go` را در 4 موقعیت مختلف اجرا کنیم.
88

9-
But calls `(1)` and `(2)` works differently from `(3)` and `(4)`. Why?
9+
ولی پاسخ موقعیت (1) و (2) با موقعیت (3) و (4) متفاوت است. چرا؟
1010

1111
```js run no-beautify
1212
let obj, method;
+39-39
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11

22
# Reference Type
33

4-
```warn header="In-depth language feature"
5-
This article covers an advanced topic, to understand certain edge-cases better.
4+
```warn header="ویژگی یک زبان عمیق"
5+
این مقاله در دسته بندی مباحث پیشرفته است, برای فهم آن باید به مباحث پایه مسلط باشید.
66
7-
It's not important. Many experienced developers live fine without knowing it. Read on if you want to know how things work under the hood.
7+
ممکن است این مباحث زیاد مهم نباشند چون خیلی از برنامه نویسان باتجربه ای هستند که کارشان به خوبی پیش میرود و این مباحث را نمیدانند. اگر علاقه مند به دانستن اینکه دستورات چگونه کار میکنند هستید بخوانید.
88
```
99

10-
A dynamically evaluated method call can lose `this`.
10+
با فراخوانی یک متد به صورت پویا و داینامیک ممکن است مقدار `this` از دست برود.
1111

12-
For instance:
12+
به عنوان مثال:
1313

1414
```js run
1515
let user = {
@@ -20,40 +20,40 @@ let user = {
2020

2121
user.hi(); // works
2222

23-
// now let's call user.hi or user.bye depending on the name
23+
// اجرا شود user.by و درغیراینصورت user.hi بود متد john حال بررسی میکنیم اگر نام برابر
2424
*!*
2525
(user.name == "John" ? user.hi : user.bye)(); // Error!
2626
*/!*
2727
```
2828

29-
On the last line there is a conditional operator that chooses either `user.hi` or `user.bye`. In this case the result is `user.hi`.
29+
در خط آخر ما شرط سه تایی (تک خطی) را داریم که براساس true یا false بودن مقدار `user.name` مشخص میشود که آیا متد `user.hi` اجرا میشود یا `user.bye`. نتیجه true است و `user.hi` برگردانده میشود.
3030

31-
Then the method is immediately called with parentheses `()`. But it doesn't work correctly!
31+
ولی چون متد پرانتز ندارد درهنگام اجرا با مشکل روبرو خواهیم بود.
3232

33-
As you can see, the call results in an error, because the value of `"this"` inside the call becomes `undefined`.
33+
همانطور که میبینید هنگام اجرا به ارور برخورد کردیم. چون مقدار `"this"` برابر با `undefined` است.
3434

35-
This works (object dot method):
35+
کد زیر کار میکند (آبجکت با عملگر نقطه):
3636
```js
3737
user.hi();
3838
```
3939

40-
This doesn't (evaluated method):
40+
اما این کد اصلا کار نمیکند! (فانکشن بدون پرانتز)
4141
```js
4242
(user.name == "John" ? user.hi : user.bye)(); // Error!
4343
```
4444

45-
Why? If we want to understand why it happens, let's get under the hood of how `obj.method()` call works.
45+
چرا؟ اگر متوجه شدید که تا اینجا چه اتفاقی افتاده, بیایید به عملکرد `()obj.method` بپردازیم.
4646

47-
## Reference type explained
47+
## توضیحات درباره Reference type
4848

49-
Looking closely, we may notice two operations in `obj.method()` statement:
49+
نگاه کنید. عبارت `()obj.method` دو عملگر داخل خود دارد:
5050

51-
1. First, the dot `'.'` retrieves the property `obj.method`.
52-
2. Then parentheses `()` execute it.
51+
1. ابتدا, عملگر نقطه `'.'` ویژگی `obj.method` را پیدا میکند.
52+
2. سپس پرانتز `()` آن را اجرا میکند.
5353

54-
So, how does the information about `this` get passed from the first part to the second one?
54+
خب پس چگونه اطلاعات مربوط به `this` از قسمت اول به دوم منتقل میشود؟
5555

56-
If we put these operations on separate lines, then `this` will be lost for sure:
56+
اگر ما این دستور را در خط های جداگانه ای قرار بدهیم, مطمئنا مقدار `this` از دست خواهد رفت:
5757

5858
```js run
5959
let user = {
@@ -62,47 +62,47 @@ let user = {
6262
};
6363

6464
*!*
65-
// split getting and calling the method in two lines
65+
// اگر به این صورت در دو خط جدا متد را صدا بزنیم:
6666
let hi = user.hi;
67-
hi(); // Error, because this is undefined
67+
hi(); // است undefiend برابر با this خطا! چون مقدار
6868
*/!*
6969
```
7070

71-
Here `hi = user.hi` puts the function into the variable, and then on the last line it is completely standalone, and so there's no `this`.
71+
در اینجا `hi = user.hi` تابع را در متغیر میریزد و چون خط آخر مستقل و از نظر جاوااسکریپت دستور جداگانه ای است به همین دلیل `this` وجود ندارد.
7272

73-
**To make `user.hi()` calls work, JavaScript uses a trick -- the dot `'.'` returns not a function, but a value of the special [Reference Type](https://tc39.github.io/ecma262/#sec-reference-specification-type).**
73+
** برای اینکه دستور `()user.hi` درست کار کند, جاوااسکریپت برای آن یک راه حلی دارد. عملگر نقطه `'.'` نه فقط فانکشن بلکه مقدار خاصی از ریفرنس تایپ ها [Reference Type](https://tc39.github.io/ecma262/#sec-reference-specification-type) را برمیگرداند **.
7474

75-
The Reference Type is a "specification type". We can't explicitly use it, but it is used internally by the language.
75+
ریفرنس تایپ ها یا نوع ارجاعی "نوع خاصی از داده ها یا specification type" هستند. ما نمیتوانیم به صورت مستقیم از آن استفاده کنیم ولی به صورت داخلی داخل زبان تعبیه شده و استفاده میشود.
7676

77-
The value of Reference Type is a three-value combination `(base, name, strict)`, where:
77+
این نوع داده خاص شامل سه مقدار `(base, name, strict)` هست که:
7878

79-
- `base` is the object.
80-
- `name` is the property name.
81-
- `strict` is true if `use strict` is in effect.
79+
- `base` یک آبجکت است
80+
- `name` نام ویژگی آبجکت هست
81+
- اگر `use strict` تعبیه شده باشد مقدار `strict` نیز true است.
8282

83-
The result of a property access `user.hi` is not a function, but a value of Reference Type. For `user.hi` in strict mode it is:
83+
نه تنها مقدار `user.hi` فانکشن نیست بلکه نوع آن از نوع Reference type هست. `user.hi` در حالت strict mode به این صورت است:
8484

8585
```js
86-
// Reference Type value
86+
// مقدار Reference Type
8787
(user, "hi", true)
8888
```
8989

90-
When parentheses `()` are called on the Reference Type, they receive the full information about the object and its method, and can set the right `this` (`user` in this case).
90+
وقتی که پرانتزی در Reference type ها صدا زده میشود, همه اطلاعات مربوط به اشیاء و متد فعلی را دریافت میکند و به درستی کلمه کلیدی `this` را مقداردهی میکند.
9191

92-
Reference type is a special "intermediary" internal type, with the purpose to pass information from dot `.` to calling parentheses `()`.
92+
نوع Reference type یک نوع داده داخلی "واسط" با هدف انتقال اطلاعات مربوطه از عملگر نقطه `.` به پرانتز است.
9393

94-
Any other operation like assignment `hi = user.hi` discards the reference type as a whole, takes the value of `user.hi` (a function) and passes it on. So any further operation "loses" `this`.
94+
عملگری مانند عملگر انتساب (Assign) در `hi = user.hi` خاصیت Reference type را در متغیر جدید کنار میذارد و صرفا مقدار تابع `user.hi` برگردانده شده را میریزد. و سپس `this` مقدار خود را از دست میدهد.
9595

96-
So, as the result, the value of `this` is only passed the right way if the function is called directly using a dot `obj.method()` or square brackets `obj['method']()` syntax (they do the same here). There are various ways to solve this problem such as [func.bind()](/bind#solution-2-bind).
96+
درنتیجه, تنها درصورتی مقدار `this` به درستی پاس میشود که مستقیما فانکشن را با استفاده از نقطه `obj.method()` یا براکت باز و بسته `obj['method']()` صدا بزنیم. راه های مختلفی برای رفع این مشکل هست مثل [func.bind()](/bind#solution-2-bind).
9797

98-
## Summary
98+
## خلاصه
9999

100-
Reference Type is an internal type of the language.
100+
تایپ مرجعی یا Reference Type ها نوع داده داخلی در زبان جاوااسکریپت هستند.
101101

102-
Reading a property, such as with dot `.` in `obj.method()` returns not exactly the property value, but a special "reference type" value that stores both the property value and the object it was taken from.
102+
وقتی که یک ویژگی یا Peraperty مخصوصا توسط عملگر نقطه `.` در `()obj.method` خوانده میشود, نه تنها مقدار ویژگی بلکه مقادیر خاص مانند ویژگی ها یا توابع خاص را هم برمیگرداند.
103103

104-
That's for the subsequent method call `()` to get the object and set `this` to it.
104+
بعد از اینکه متدی فراخوانی شد, عملگر پرانتز آبجکت را دریافت و `this` را مقداردهی میکند.
105105

106-
For all other operations, the reference type automatically becomes the property value (a function in our case).
106+
برای مابقی عملگر ها Reference Type تبدیل به مقدار ویژگی میشود (تابع هم همینطور).
107107

108-
The whole mechanics is hidden from our eyes. It only matters in subtle cases, such as when a method is obtained dynamically from the object, using an expression.
108+
همه مکانیزم هایی که از چشم ما پنهان هستند, خیلی کم پیش میاید که از آن را مهم بدانیم و مطلع شویم که چطور کار میکنند. مانند زمانی که یک مقد به صورت پویا از طریق آبجکت و با استفاده از عبارت ها به دست میاید.

0 commit comments

Comments
 (0)