FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
javascript-patterns/general-patterns/avoiding-eval.html at master · webwebwebweb/javascript-patterns · GitHub
webwebwebweb
javascript-patterns
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
javascript-patterns
/
general-patterns
/
avoiding-eval.html
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (35 loc) · 1015 Bytes
Breadcrumbs
javascript-patterns
/
general-patterns
/
avoiding-eval.html
Copy path
File metadata and controls
43 lines (35 loc) · 1015 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
>
Back
|
FazBrowse Home
|
New Git URL