3 ms·
My biggest hurdle with JavaScript was getting things to happen in order. Functions can be unexpectedly colorful[0] and contortions and boilerplate is needed to
by giantrobot 5y ago
My biggest hurdle with JavaScript was getting things to happen in order. Functions can be unexpectedly colorful[0] and contortions and boilerplate is needed to deal with that. What was unintuitive to me was a lot of interactions with the DOM are asynchronous without being explicit about it. So you've got to chain a bunch of asynchronous calls together to make sure a sequence of events occur. Most other languages make this sort of asynchronous callback hell explicit.
Once you understand this aspect of the language it's an annoying (to me) idiosyncrasy but manageable. Before you understand it there's a lot of pouring over poorly written tutorials that gloss over it like it should be obvious.
[0] https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/ https://journal.stuffwithstuff.com/2015/02/01/what-color-is-...
- simlevesque 5y agoDo you have an example of a DOM operation that is async ?
- giantrobot 5y agoCreating a new element on the DOM, especially when it is creating a new script element. It takes non-zero time to load and parse but returns control to JavaScript immediately. So you need to wait on the element with like onLoad before calling a function in that script. There's some more instances I've forgotten. The situation is better now with promises but it was a pain in the ass just a few years ago.
- simlevesque 5y agoWell, yeah, some DOM elements have async side effect, but they are still added to the DOM synchronously. Creating the element and loading it's content are two completely separate things.
- giantrobot 5y agoThat's non-obvious to people from other languages, at least it was to me. The asynchronous nature of DOM manipulation also comes up when the DOM gets large and you naively try updating elements querying by tag or class. It's idiosyncrasies aren't deal breakers but they're non-obvious. They also crop up once you're into medium complexity code. At the level of simple/trivial code machines and JavaScript engines are fast enough to paper over naively written code. But you get to a certain level of complexity and you get bit by some colored function issue or something. It's annoying bringing something up where JavaScript is not the primary focus of the project. This is all anecdotal, there's plenty of people that might never have issues. But it was all stuff that tripped me up around 2009-10 having to write some JS after not having touched it since before you could make XHRs or manipulate the DOM directly.
- eckmLJE 5y agoIf you were developing in JS in 2015 this hurdle is understandable.
- z3t4 5y agoAfter a year or so writing "async" code it has become natural. I now think of the real world as async. For example during Christmas I do not "poll" the door every 10 minute to see Santa clause. I simply wait until he knocks on the door and then I run the knockOnDoor callback function. So in my mind functions did not have colors. They where "first class functions" that you could pass into another function that later called it. Now however we do have normal functions (before ES6-7), as well as async/await promise returning functions, and *function yealding generator functions.