HTML and CSS Reference
In-Depth Information
Chapter 1
JavaScript Is Not the Language You
Think It Is
Sean Bennett, Course Architect, Udacity
JavaScript is a deceptively familiar language. Its syntax is close enough to C/C++ that you may be tricked into thinking
it behaves similarly.
However, JavaScrit has a number of gotchas that trip up developers coming from other languages. In this chapter,
I'll go over some of the more egregious offenders, teach you how to avoid them, and showcase the hidden power in
the language.
This chapter is for game programmers who are coming from other languages and who are using JavaScript for the
first time. It's the tool I wish I'd had when I first started using JavaScript.
Variables and Scoping Rules
You wouldn't think that declaring variables would be at all hard or error prone. After all, it's a fundamental part of any
language, right? The problem is that with JavaScript, it's
very easy to accidentally declare variables after they're used, which leads to accidentally
accessing undefined variables
deceptively difficult to restrict access to variables, leading to naming collisions as well as
memory allocation issues
I'll discuss the issues with and limitations of JavaScript scoping and then present a well-known solution for
modularizing your JavaScript code.
Declaration Scoping
The first thing you need to realize about JavaScript is that there are only two different scopes: global and function
level. JavaScript does not have any further lexical or block scoping.
A variable is declared on the global scope like so:
zombiesKilled = 10;
A variable is declared on the function scope as follows:
var antiPokemonSpray = true;
Search WWH ::

Custom Search