Skip to content

Commit b9ace53

Browse files
authored
Merge pull request #299 from mostafa-nematpour/modules
Dynamic imports
2 parents fd3d69e + 8838005 commit b9ace53

File tree

1 file changed

+21
-21
lines changed
  • 1-js/13-modules/03-modules-dynamic-imports

1 file changed

+21
-21
lines changed
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
# Dynamic imports
1+
# Import پویا
22

3-
Export and import statements that we covered in previous chapters are called "static". The syntax is very simple and strict.
3+
عبارات Export و Import که در بخش‌های قبلی بررسی کردیم "ایستا (static)" نامیده می‌شوند. ساختار آنها بسیار ساده و سخت‌گیرانه است.
44

5-
First, we can't dynamically generate any parameters of `import`.
5+
اول اینکه، نمی‌توانیم پارامترهای `import` را به صورت پویا تولید کنیم.
66

7-
The module path must be a primitive string, can't be a function call. This won't work:
7+
مسیر ماژول باید یک رشته ابتدایی باشد، نمی تواند یک فراخوانی تابع باشد. این کار نمی‌کند:
88

99
```js
1010
import ... from *!*getModuleName()*/!*; // Error, only from "string" is allowed
1111
```
1212

13-
Second, we can't import conditionally or at run-time:
13+
دوم اینکه، نمی‌توانیم به صورت شرطی یا در زمان اجرا آن را import کنیم:
1414

1515
```js
1616
if(...) {
17-
import ...; // Error, not allowed!
17+
import ...; // !خطا، مجاز نیست
1818
}
1919

2020
{
21-
import ...; // Error, we can't put import in any block
21+
import ...; // را در هر بلوکی قرار دهیم import خطا، نمی‌توانیم
2222
}
2323
```
2424

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 ساده و ثابت است.
2626

27-
But how can we import a module dynamically, on-demand?
27+
اما چگونه می‌توان یک ماژول را به صورت پویا، بنا به نیازمان import کنیم؟
2828

29-
## The import() expression
29+
## عبارت import()
3030

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 های ایستا دقت کنید)
3232

33-
We can use it dynamically in any place of the code, for instance:
33+
می‌توانیم آن را به صورت پویا در هر جای کد استفاده کنیم، به عنوان مثال:
3434

3535
```js
3636
let modulePath = prompt("Which module to load?");
@@ -40,9 +40,9 @@ import(modulePath)
4040
.catch(err => <loading error, e.g. if no such module>)
4141
```
4242

43-
Or, we could use `let module = await import(modulePath)` if inside an async function.
43+
یا، می‌توانیم `let module = await import(modulePath)` را درون یک تابع هنگام (async) استفاده کنیم.
4444

45-
For instance, if we have the following module `say.js`:
45+
به عنوان مثال، اگر ماژول `say.js` را به شرح زیر داشته باشیم:
4646

4747
```js
4848
// 📁 say.js
@@ -55,7 +55,7 @@ export function bye() {
5555
}
5656
```
5757

58-
...Then dynamic import can be like this:
58+
آنگاه import پویا می‌تواند مانند این باشد:
5959

6060
```js
6161
let {hi, bye} = await import('./say.js');
@@ -64,7 +64,7 @@ hi();
6464
bye();
6565
```
6666

67-
Or, if `say.js` has the default export:
67+
یا اگر `say.js` دارای export پیش‌فرض باشد:
6868

6969
```js
7070
// 📁 say.js
@@ -73,7 +73,7 @@ export default function() {
7373
}
7474
```
7575

76-
...Then, in order to access it, we can use `default` property of the module object:
76+
آنگاه برای دسترسی به آن، می‌توانیم از خاصیت `default` شیء ماژول استفاده کنیم:
7777

7878
```js
7979
let obj = await import('./say.js');
@@ -83,16 +83,16 @@ let say = obj.default;
8383
say();
8484
```
8585

86-
Here's the full example:
86+
مثال کامل:
8787

8888
[codetabs src="say" current="index.html"]
8989

9090
```smart
91-
Dynamic imports work in regular scripts, they don't require `script type="module"`.
91+
import پویا در اسکریپت‌های معمولی هم کار می‌کنند، نیازی به `script type="module"‎` ندارند.
9292
```
9393

9494
```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()`).
9696
97-
So we can't copy `import` to a variable or use `call/apply` with it. It's not a function.
97+
پس نمی‌توانیم آن را به یک متغیر اختصاص دهیم یا از `call/apply` در رفتار با آن استفاده کنیم. تابع نیست.
9898
```

0 commit comments

Comments
 (0)