Live data from Hacker News

Common JavaScript Gotchas

jblotus.com

1–10 of 28 posts

Common JavaScript Gotchas

#1
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.

Common JavaScript Gotchas
jblotus.com

Re: Common JavaScript Gotchas

#7
Using closures to encapsulate private variables is a good idea, but the way this article recommends doing it doesn't demonstrate any of the benefit as it doesn't really close over anything. I don't know if it's an example of good JS code, but something like the basic closure definition of a counter might have better demonstrated the ability to hide state:

    var counter = (function () {
        var i = 0;

        return function () {
            i += 1;
            return i;
        }
    }());
Where the internal variable, i, is inaccessible to anything but the counter function itself.

Re: Common JavaScript Gotchas

#8
post #7

Using closures to encapsulate private variables is a good idea, but the way this article recommends doing it doesn't demonstrate any of the benefit as it doesn't really close over anything. I don't know if it's an example of good JS code, but something like the basic closure definition of a counter might have better demonstrated the ability to hide state: var counter = (function () { var i = 0; return function () { i…

author here, it wasn't about hiding private variables. i was demonstrating using closures to avoid global namespace collisions which i think is generally a big problem for beginners.

Re: Common JavaScript Gotchas

#9

That's why developers should always lint their code. www.jslint.com

Or http://www.jshint.com/ if they feel jslint is too harsh or arbitrary about some things.

jshint seems to be more actively developed, more receptive to changes, and more flexible in terms of instructions (like ignoring certain patterns)

Re: Common JavaScript Gotchas

#10
post #8
post #7

Using closures to encapsulate private variables is a good idea, but the way this article recommends doing it doesn't demonstrate any of the benefit as it doesn't really close over anything. I don't know if it's an example of good JS code, but something like the basic closure definition of a counter might have better demonstrated the ability to hide state: var counter = (function () { var i = 0; return function () { i…

author here, it wasn't about hiding private variables. i was demonstrating using closures to avoid global namespace collisions which i think is generally a big problem for beginners.

i believe you mean using functions, not closures. your post doesn't seem to have the same misnomer, and simply explains lexical function scope. it doesn't seem to touch on the closure, while your parent comment does.
Post reply on HN