forked from chuanxshi/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avoiding-eval.html
43 lines (35 loc) · 1015 Bytes
/
avoiding-eval.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Avoiding eval()
* Description: avoid using eval()
*/
// antipattern 1
var property = "name";
alert(eval("obj." + property));
// preferred 1
var property = "name";
alert(obj[property]);
/* antipattern 2
* It's also important to remember that passing strings to setInterval(), setTimeout(),
* and the Function() constructor is, for the most part, similar to using eval() and therefore should be avoided.
*/
setTimeout("myFunc()", 1000);
setTimeout("myFunc(1, 2, 3)", 1000);
// preferred 2
setTimeout(myFunc, 1000);
setTimeout(function () {
myFunc(1, 2, 3);
}, 1000);
// in supported browsers (i.e. not IE)
setTimeout(myFunc, 1000, 1, 2, 3);
// References
// http://net.tutsplus.com/tutorials/javascript-ajax/the-essentials-of-writing-high-quality-javascript/
</script>
</body>
</html>