Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
45 lines (35 sloc) 1022 Bytes
<!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",
obj = { name: "John" };
alert(eval("obj." + property));
// preferred 1
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>