Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions 1-js/99-js-misc/02-eval/article.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# Eval: run a code string
# Eval: یک رشته را به عنوان کد اجرا کنید

The built-in `eval` function allows to execute a string of code.
تابع درونی `eval` اجازه می دهد تا یک رشته از کد را اجرا کنید

The syntax is:
دستور:

```js
let result = eval(code);
```

For example:
به عنوان مثال:

```js run
let code = 'alert("Hello")';
eval(code); // Hello
```

A string of code may be long, contain line breaks, function declarations, variables and so on.
رشته می تواند طولانی و چند خطی باشد، تابع یا متغییر اعلان کند و...

The result of `eval` is the result of the last statement.
خروجی `eval` همان نتیجه آخرین عملیات است

For example:
برای نمونه:
```js run
let value = eval('1+1');
alert(value); // 2
Expand All @@ -30,7 +30,7 @@ let value = eval('let i = 0; ++i');
alert(value); // 1
```

The eval'ed code is executed in the current lexical environment, so it can see outer variables:
رشته در محیط جاری اجرا می شود بنابراین به متغییر های خارجی در **Scope** دسترسی دارد:

```js run no-beautify
let a = 1;
Expand All @@ -46,15 +46,15 @@ function f() {
f();
```

It can change outer variables as well:
همچنین می تواند متغییر های خارجی را نیز تغییر دهد:

```js untrusted refresh run
let x = 5;
eval("x = 10");
alert(x); // 10, value modified
```

In strict mode, `eval` has its own lexical environment. So functions and variables, declared inside eval, are not visible outside:
در `strict mode`, `eval` محیط خاص خودش را دارد. بنابراین توابع و متغییر های اعلان شده در `eval` در خارج قابل دسترسی نیستند:

```js untrusted refresh run
// reminder: 'use strict' is enabled in runnable examples by default
Expand All @@ -65,27 +65,27 @@ alert(typeof x); // undefined (no such variable)
// function f is also not visible
```

Without `use strict`, `eval` doesn't have its own lexical environment, so we would see `x` and `f` outside.
بدون `use strict`, `eval` محیط خاص خودش را ندارد, بر این اساس ما `x` و `f` در خارج قابل دسترسی اند.

## Using "eval"
## استفاده از "eval"

In modern programming `eval` is used very sparingly. It's often said that "eval is evil".
در برنامه نویسی مدرن `eval` به ندرت استفاده میشود. به طور معمول گفته می شود "eval is evil".

The reason is simple: long, long time ago JavaScript was a much weaker language, many things could only be done with `eval`. But that time passed a decade ago.
دلیلش ساده است: خیلی وقت پیش جاوااسکریپت یک زبان بسیار ضعیف تر بود، خیلی چیز ها تنها می توانستند با `eval` انجام شوند اما یک دهه از آنگاه می گذرد.

اکنون, هیچ دلیلی برای استفاده از `eval` نیست. اگر کسی از آن استفاده می کند، میتواند برای جایگزین کردن آن از ساخت های مدرن زبان یا [JavaScript Module](info:modules) بهره بگیرد.

Right now, there's almost no reason to use `eval`. If someone is using it, there's a good chance they can replace it with a modern language construct or a [JavaScript Module](info:modules).
لطفا توجه کنید که قابلیت آن برای دسترسی به متغییر های خارجی عوارض جانبی دارد.

Please note that its ability to access outer variables has side-effects.
) فشرده کنندگان کدCode minifiers( متغیر های محلی را به نام های کوتاه تر تغییر نام می دهند (مانند `a`و `b`) برای کوتاه کردن کد. این کار معمولا مشکلی ایجاد نمی کند اما نه وقتی که `eval` استفاده شده, از آنجا که متغییر های محلی شاید از طریق `eval` مورد استفاده قرار گرفته باشند بنابراین خرد کنندگان متغییر هایی که می توانند توسط `eval` مورد استفاده قرا گرفته باشند را تغییر نمی دهند که باعث کاهش ضریب فشرده سازی می شود.

Code minifiers (tools used before JS gets to production, to compress it) rename local variables into shorter ones (like `a`, `b` etc) to make the code smaller. That's usually safe, but not if `eval` is used, as local variables may be accessed from eval'ed code string. So minifiers don't do that renaming for all variables potentially visible from `eval`. That negatively affects code compression ratio.
استفاده از متغییر های درون `eval` در خارج از آن همچنین نامناسب است, چراکه نگهداری کد را سخت تر می کند.

Using outer local variables inside `eval` is also considered a bad programming practice, as it makes maintaining the code more difficult.
دو را برای ایمن ماندن از چنین مشکلاتی وجود دارد.

There are two ways how to be totally safe from such problems.
**اگر `eval` از متغییر های خارجی استفاده نمی کند, لطفا `eval` را به صورت `window.eval(...)` فرا بخوانید:**

**If eval'ed code doesn't use outer variables, please call `eval` as `window.eval(...)`:**

This way the code is executed in the global scope:
در این صورت رشته در محیط عمومی (global scope) اجرا می شود

```js untrusted refresh run
let x = 1;
Expand All @@ -95,20 +95,20 @@ let x = 1;
}
```

**If eval'ed code needs local variables, change `eval` to `new Function` and pass them as arguments:**
**اگر رشته کد نیاز به متغییر های خارجی دارد, `eval` را به `new Function` تغییر دهید و آنها را به عنوان آرگومان به آن پاس دهید:**

```js run
let f = new Function('a', 'alert(a)');

f(5); // 5
```

The `new Function` construct is explained in the chapter <info:new-function>. It creates a function from a string, also in the global scope. So it can't see local variables. But it's so much clearer to pass them explicitly as arguments, like in the example above.
`new Function` در بخش توضیح داده شده است <info:new-function>. آن یک تابع با استفاده از یک رشته ایجاد می کند, همچنین در محیط عمومی. بنابراین آن نمی تواند دیگر متغییر های محلی را ببیند اما آن بسیار بهتر است که آنها صریحا به عنوان آرگومان پاس دهیم، مانند مثال بالا.

## Summary
## جمع بندی

A call to `eval(code)` runs the string of code and returns the result of the last statement.
- Rarely used in modern JavaScript, as there's usually no need.
- Can access outer local variables. That's considered bad practice.
- Instead, to `eval` the code in the global scope, use `window.eval(code)`.
- Or, if your code needs some data from the outer scope, use `new Function` and pass it as arguments.
فراخوانی `eval(code)` رشته کد را اجرا می کند و نتیجه آخرین عملیات را بر می گرداند.
- به ندرت در جاوااسکریپت مدرن مورد استفاده است, چراکه معمولا نیازی به آن نیست.
- می تواند به متغییر های محلی خارجی دسترسی یابد. که نامناسب در نظر گرفته شده.
- به جای `eval` کد را در محیط عمومی اجرا کنید و از `window.eval(code)` استفاده کنید.
- یا اگر کد شما داده هایی از محیط خارجی نیاز دارد از `new Function` استفاده کنید و به عنوان آرگومان ها به آن پاس دهید.