Common JavaScript Gotchas (jblotus.com)
PHP was my first programming language, and my initial exposure to JavaScript was through libraries like jQuery. There were things about JavaScript that always seemed to trip me up in the beginning due to how they worked differently than PHP. Heck there are still some things today that are confusing. I want to share some of the things that I struggled when I started working with JavaScript. I am going to cover the global namespace, this, knowing the difference between ECMAScript 3 and ECMAScript 5, asynchronous operations, prototypes, and simple JavaScript inheritance.
27 comments
[ 3.1 ms ] story [ 64.0 ms ] threadif your function doesn't return another function, the references from that function are deallocated once the function has executed.
when your function returns a function (a "closure"), the inner function closes over the variables of the outer function, maintaining references to it's context as long as you maintain a reference to the closure.
This can manifest itself in many ways. Usually in the form of a UI element being displayed more than once, but often more subtly such as a HTTP request being sent more than once. Often the bugs only manifest themselves after a certain sequence of actions, for example: click this UI element, check this radio box, then hit submit and watch the net tab.
It's so often the root cause of Javascript bugs that if I or somebody else on my team has a Javascript problem, I often start by asking myself if it looks like it could be this. Lately I have begun to wonder if there isn't some slight truth to that inflammatory "Callbacks are the new goto" story that was on here a couple of months ago [1].
[1] http://elm-lang.org/learn/Escape-from-Callback-Hell.elm
I've yet to run into this problem before, and I'm an even split between JS / Ruby these days. Are you building on top of any particular libraries or frameworks? Could you give some examples of where you've encountered this?
The other time was in a Rails app, where a script was added to a partial for a layout, and then added again later on (by a different developer) to the layout itself. That caused a particularly nasty bug since it used 'toggle', and thus the second callback negated the first.
I'm not saying it's a good thing to work around the issue by using event filtering, but events filtering is a great technique.
You can also use your event filtering to "fail fast" during development: detect a duplicated event called back? Throw an exception / make your application fail.
This is not true, the var hoist the variable and scopes it to the function, but does not create it as a property of the function. A function is defined in its outer function scope, an it's properties will persist across calls.
Adding properties to functions is useful for memoization, but is different than what the var statement does.Your summary of that section is a better explanation:
all variables are scoped to a function (which is itself an object), and where you declare those variables with var determines the function they are scoped to.
You might add, if you never declare the variable with var it is implicitly declared global.
<?php $a = 1; /* global scope */
function test() { echo $a }
test();
in javascript, $a would be 1 but in PHP it is undefined and won't produce output,