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
Export and import statements that we covered in previous chapters are called "static". The syntax is very simple and strict.
3
+
عبارات Export و Import که در بخشهای قبلی بررسی کردیم "ایستا (static)" نامیده میشوند. ساختار آنها بسیار ساده و سختگیرانه است.
4
4
5
-
First, we can't dynamically generate any parameters of `import`.
5
+
اول اینکه، نمیتوانیم پارامترهای `import` را به صورت پویا تولید کنیم.
6
6
7
-
The module path must be a primitive string, can't be a function call. This won't work:
7
+
مسیر ماژول باید یک رشته ابتدایی باشد، نمی تواند یک فراخوانی تابع باشد. این کار نمیکند:
8
8
9
9
```js
10
10
import ... from*!*getModuleName()*/!*; // Error, only from "string" is allowed
11
11
```
12
12
13
-
Second, we can't import conditionally or at run-time:
13
+
دوم اینکه، نمیتوانیم به صورت شرطی یا در زمان اجرا آن را import کنیم:
14
14
15
15
```js
16
16
if(...) {
17
-
import ...; //Error, not allowed!
17
+
import ...; //!خطا، مجاز نیست
18
18
}
19
19
20
20
{
21
-
import ...; //Error, we can't put import in any block
21
+
import ...; //را در هر بلوکی قرار دهیم import خطا، نمیتوانیم
22
22
}
23
23
```
24
24
25
-
That's because `import`/`export`aim to provide a backbone for the code structure. That's a good thing, as code structure can be analyzed, modules can be gathered and bundled into one file by special tools, unused exports can be removed ("tree-shaken"). That's possible only because the structure of imports/exports is simple and fixed.
25
+
زیرا `import`/`export`قصد دارد ستون فقراتی برای ساختار کد فراهم کنند. این یک چیز خوب است، زیرا ساختار کد قابل تجزیه و تحلیل است، ماژول ها را میتوان با ابزارهای ویژه در یک قالب یک فایل جمع آوری کرد، export های استفاده نشده میتوانند حذف شوند ("tree-shaken"). اینها فقط به این خاطر امکانپذیر است که ساختار imports/exports ساده و ثابت است.
26
26
27
-
But how can we import a module dynamically, on-demand?
27
+
اما چگونه میتوان یک ماژول را به صورت پویا، بنا به نیازمان import کنیم؟
28
28
29
-
## The import() expression
29
+
## عبارت import()
30
30
31
-
The`import(module)`expression loads the module and returns a promise that resolves into a module object that contains all its exports. It can be called from any place in the code.
31
+
عبارت`import(module)`ماژول را بارگذاری میکند و یک promise برمیگرداند که به یک شی حاوی همه export های ماژول تبدیل میشود. میتوان آن را در هر جایی از کد صدا زد. (به تفاوت ظاهری آن با import های ایستا دقت کنید)
32
32
33
-
We can use it dynamically in any place of the code, for instance:
33
+
میتوانیم آن را به صورت پویا در هر جای کد استفاده کنیم، به عنوان مثال:
34
34
35
35
```js
36
36
let modulePath =prompt("Which module to load?");
@@ -40,9 +40,9 @@ import(modulePath)
40
40
.catch(err=><loading error, e.g. if no such module>)
41
41
```
42
42
43
-
Or, we could use `let module = await import(modulePath)`if inside an async function.
43
+
یا، میتوانیم `let module = await import(modulePath)`را درون یک تابع هنگام (async) استفاده کنیم.
44
44
45
-
For instance, if we have the following module `say.js`:
45
+
به عنوان مثال، اگر ماژول `say.js` را به شرح زیر داشته باشیم:
46
46
47
47
```js
48
48
// 📁 say.js
@@ -55,7 +55,7 @@ export function bye() {
55
55
}
56
56
```
57
57
58
-
...Then dynamic import can be like this:
58
+
آنگاه import پویا میتواند مانند این باشد:
59
59
60
60
```js
61
61
let {hi, bye} =awaitimport('./say.js');
@@ -64,7 +64,7 @@ hi();
64
64
bye();
65
65
```
66
66
67
-
Or, if`say.js`has the default export:
67
+
یا اگر`say.js`دارای export پیشفرض باشد:
68
68
69
69
```js
70
70
// 📁 say.js
@@ -73,7 +73,7 @@ export default function() {
73
73
}
74
74
```
75
75
76
-
...Then, in order to access it, we can use `default`property of the module object:
76
+
آنگاه برای دسترسی به آن، میتوانیم از خاصیت `default`شیء ماژول استفاده کنیم:
77
77
78
78
```js
79
79
let obj =awaitimport('./say.js');
@@ -83,16 +83,16 @@ let say = obj.default;
83
83
say();
84
84
```
85
85
86
-
Here's the full example:
86
+
مثال کامل:
87
87
88
88
[codetabs src="say" current="index.html"]
89
89
90
90
```smart
91
-
Dynamic imports work in regular scripts, they don't require `script type="module"`.
91
+
import پویا در اسکریپتهای معمولی هم کار میکنند، نیازی به `script type="module"` ندارند.
92
92
```
93
93
94
94
```smart
95
-
Although `import()` looks like a function call, it's a special syntax that just happens to use parentheses (similar to `super()`).
95
+
اگرچه import() شبیه یک تابع به نظر میرسد، ولی ساختار ویژهای است که تصادفاً از پرانتز استفاده میکند (مشابه `super()`).
96
96
97
-
So we can't copy `import` to a variable or use `call/apply` with it. It's not a function.
97
+
پس نمیتوانیم آن را به یک متغیر اختصاص دهیم یا از `call/apply` در رفتار با آن استفاده کنیم. تابع نیست.
0 commit comments