Permalink
Switch branches/tags
Nothing to show
Find file Copy path
5ae4f98 Jan 21, 2014
2 contributors

Users who have contributed to this file

@shichuan @shaunmccarthy
42 lines (38 sloc) 1.23 KB
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Hoisting
* Description: var statements anywhere in a function act as if the variables were declared at the top of the function
*/
// antipattern
wontchange = "global"; // global variable
willchange = "global"; // global variable
function func_bad() {
alert(wontchange); // "global"
alert(willchange); // "undefined" due to the var below
var willchange = "local"; // This rescopes willchange inside this function
alert(willchange); // "local"
}
func_bad();
// the preceding code snippet will behave as if it were implemented like so:
wontchange = "global"; // global variable
willchange = "global"; // global variable
function func() {
// This still rescopes, but it is a lot more explicit
var willchange; // same as -> var willchange = undefined;
alert(wontchange); // "global"
alert(willchange); // "undefined"
willchange = "local";
alert(willchange); // "local"
}
func();
// References
// http://net.tutsplus.com/tutorials/javascript-ajax/the-essentials-of-writing-high-quality-javascript/
</script>
</body>
</html>