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
Let's use `eval` to calculate the maths expression:
1
+
`eval`ı bu matematiksel ifadeyi hesaplamakta kullanalım:
2
2
3
3
```js demo run
4
-
let expr =prompt("Type an arithmetic expression?", '2*3+2');
4
+
let expr =prompt("Aritmetik bir ifade girin", '2*3+2');
5
5
6
6
alert( eval(expr) );
7
7
```
8
8
9
-
The user can input any text or code though.
9
+
Kullanıcı herhangi bir metin veya kod girebilir.
10
10
11
-
To make things safe, and limit it to arithmetics only, we can check the `expr`using a [regular expression](info:regular-expressions), so that it only may contain digits and operators.
11
+
Bunları güvenli hale getirip yalnızca aritmetiksel ifadelerle sınıflandırabilmek için `expr`değişkenini [düzenli ifadeler](info:regular-expressions) kullanarak kontrol edebiliriz, böylece ifade yalnızca rakam ve operatör içerebilecektir.
The built-in `eval`function allows to execute a string of `code`.;
3
+
Yerleşik `eval`fonksiyonu, `kod` şeklindeki bir karakter dizisini çalıştırmayı sağlar.
4
4
5
-
The syntax is:
5
+
Sözdizimi şu şekildedir:
6
6
7
7
```js
8
8
let result =eval(code);
9
9
```
10
10
11
-
For example:
11
+
Örneğin:
12
12
13
13
```js run
14
-
let code ='alert("Hello")';
15
-
eval(code); //Hello
14
+
let code ='alert("Esenlikler")';
15
+
eval(code); //Esenlikler
16
16
```
17
17
18
-
A call to `eval`returns the result of the last statement.
18
+
Bir `eval`çalıştırmak son ifadenin sonucunu döndürür.
19
19
20
-
For example:
20
+
Örneğin:
21
21
```js run
22
22
let value =eval('1+1');
23
23
alert(value); // 2
24
24
```
25
25
26
-
The code is executed in the current lexical environment, so it can see outer variables:
26
+
Kod, o anki sözcüksel ortamda yürütülür, bu nedenle dış değişkenlere erişebilir.
27
27
28
28
```js run no-beautify
29
29
let a =1;
@@ -39,69 +39,68 @@ function f() {
39
39
f();
40
40
```
41
41
42
-
It can change outer variables as well:
42
+
Aynı şekilde dış değişkenleri de değiştirebilir:
43
43
44
44
```js untrusted refresh run
45
45
let x =5;
46
46
eval("x = 10");
47
-
alert(x); // 10, value modified
47
+
alert(x); // 10, değer değişti
48
48
```
49
49
50
-
In strict mode, `eval`has its own lexical environment. So functions and variables, declared inside eval, are not visible outside:
50
+
Katı modda `eval`kendi sözcüksel ortamına sahiptir. Bu nedenle eval içerisinde tanımlanan fonksiyon ve değişkenler dışarıdan ulaşılabilir değildir.
51
51
52
52
```js untrusted refresh run
53
-
//reminder: 'use strict' is enabled in runnable examples by default
53
+
//hatırlatma: çalıştırılabilir örneklerde 'use strict' varsayılan olarak etkin durumdadır.
54
54
55
55
eval("let x = 5; function f() {}");
56
56
57
-
alert(typeof x); // undefined (no such variable)
58
-
//function f is also not visible
57
+
alert(typeof x); // undefined (böyle bir değişken yok)
58
+
//f fonksiyonu da aynı şekilde ulaşılmaz durumda
59
59
```
60
60
61
-
Without `use strict`, `eval` doesn't have its own lexical environment, so we would see `x`and`f`outside.
61
+
`use strict` kullanılmadığı takdirde `eval`, kendi sözcüksel ortamına sahip değildir, bu yüzden `x`ve`f`ifadelerini dışarıdan görebiliriz.
62
62
63
-
## Using "eval"
63
+
## "Eval" kullanımı
64
64
65
-
In modern programming `eval` is used very sparingly. It's often said that "eval is evil".
65
+
Modern programlamada `eval`, oldukça cüzi miktarda kullanılır. Kendisinden çoğunlukla "eval is evil" (eval kötüdür) şeklinde bahsedilir.
66
66
67
-
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.
67
+
Nedeni oldukça basit: uzun, çok uzun zaman önce JavaScript, çoğu şeyin yalnızca `eval` ile yapılabildiği, oldukça zayıf bir dildi. Fakat bu artık on yıl kadar öncede kaldı.
68
68
69
-
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).
69
+
Şu an `eval` kullanmak için neredeyse hiçbir neden bulunmuyor. Eğer birisi kullanıyorsa bunu modern bri dil yapısıyla veya bir [JavaScript Modülü](info:modules) ile değiştirmek için iyi bir fırsatı var.
70
70
71
-
Still, if you're sure you need to dynamically `eval`a string of code, please note that its ability to access outer variables has side-effects.
71
+
Halen dinamik bir `eval`karakter dizisi şeklinde bir koda ihtiyacınız varsa lütfen bunun dış değişkenlere yan etkilere neden olarak erişebileceğinin farkında olun.
72
72
73
-
Code minifiers (tools used before JS gets to production, to compress it) replace local variables with shorter ones for brewity. That's usually safe, but not if `eval`is used, as it may reference them. So minifiers don't replace all local variables that might be visible from `eval`. That negatively affects code compression ratio.
73
+
Kod küçültücüler (minifiers - JS kodlarını yayınlamadan önce sıkıştıran araçlar) yerel değişkenleri üretim için kısa olanlarıyla değiştirir. Bu genellikle güvenlidir, şayet birçok referansa sahip `eval`kullanılmıyorsa. Dolayısıyla küçültücüler `eval`dan görülebilen tüm yerel değişkenleri değiştirmez. Bu, kod sıkıştırma oranını büyük oranda kötü etkileyecektir.
74
74
75
-
Using outer local variables inside `eval` is a bad programming practice, as it makes maintaining the code more difficult.
75
+
`eval`ın içinde dış yerel değişkenler kullanmak kod kontrolünü zorlaştıran kötü bir programlama yöntemidir.
76
76
77
-
There are two ways how to evade any eval-related problems.
77
+
Eval ile bağlantılı sorunlardan kaçınmanın iki adet yolu mevcut.
78
78
79
-
**If eval'ed code doesn't use outer variables, please call `eval` as `window.eval(...)`:**
79
+
**Eğer eval'laştırılmış kod dış değişkenleri kullanmıyorsas lütfen `eval`ı `window.eval(...)` şeklinde kullanın:**
80
80
81
-
This way the code is executed in the global scope:
81
+
Bu yöntem, kodu global kapsamda çalıştıracaktır.
82
82
83
83
```js untrusted refresh run
84
84
let x =1;
85
85
{
86
86
let x =5;
87
-
window.eval('alert(x)'); // 1 (global variable)
87
+
window.eval('alert(x)'); // 1 (global değişken)
88
88
}
89
89
```
90
90
91
-
**If your code needs local variables, execute it with `new Function`and pass them as arguments:**
91
+
**Eğer kod yerel değişkenlere ihtiyaç duyuyorsa `new Function`ile çalıştırın ve bunları argüman olarak geçirin:**
92
92
93
93
```js run
94
94
let f =newFunction('a', 'alert(a)');
95
95
96
96
f(5); // 5
97
97
```
98
98
99
-
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.
99
+
`new Function`yapısı <info:new-function> bölümünde açıklanmıştır. Bu, bir karakter dizisinden, aynı zamanda global kapsamda olan bir fonksiyon yaratır. Bu yüzden yerel değişkenleri göremez. Fakat yukarıdaki örnekte de görülebileceği üzere bunları açık şekilde argüman olarak göndermek çok daha temiz bir yoldur.
100
100
101
-
## Summary
101
+
## Özet
102
102
103
-
A call to `eval(code)` runs the string of code and returns the result of the last statement.
104
-
- Rarely used in modern JavaScript, as there's usually no need.
105
-
- Can access outer local variables. That's considered bad practice.
106
-
- Instead, to `eval` the code in the global scope, use `window.eval(code)`.
107
-
- Or, if your code needs some data from the outer scope, use `new Function` and pass it as arguments.
103
+
`eval(code)` yapısı karakter dizisi formatındaki bir kodu çalıştırır ve son ifadenin sonucunu döndürür.
104
+
- Olabildiğince az ihtiyaç duyularak modern JavaScript'te nadiren kullanılır.
105
+
- Global kapsamda `eval` kullanmak yerine `window.eval(code)` kullanın.
106
+
- Veya kodunuz dış kapsamdan bazı verilere ihtiyaç duyuyorsa `new Function` kullanın ve bunları argüman olarak gönderin.
0 commit comments