Scope is not necessarily an intuitive thing in Javascript, as far as I'm concerned. But, I thought I'd do a little experimentation and write up one of my quick little notes pages to keep handy a reminder for myself. Sometimes, when you switch projects, you work more in one language (ActionScript or PHP) and less in another (Javascript or C). I will be the first to admit that I can't always remember the little idiosyncrasies of every language that I know.
Take a look at this code:
Here is the output:
The main items to point out in that code above are the two similarly named functions, "doubleC" and, "cDoubled." The cDoubled function is actually assigned to the "Thing" object that contains it. The doubleC function is simply sitting inside the Thing object, but isn't accessible outside of it. In other words, you cannot make this call:
It gives an error: "Error: t.doubleC is not a function". Ick. Errors are such a pain.
So, basically, the simple rule is (without getting into the deep, deep mechanics of Javascript and the ECMAScript engine and stuff) that if you want the function to be a real method of the class, you want to declare it with the "this.[functionName] = function ()" anonymous function declaration.