Lurker News

Comments (110) 18 threads

Earlier comments
scottmf2 replies#
Looks great. I love modern JS, and the faster everyone can move on, the better!

I didn't see any async/await stuff though, is there a reason for that? I'd imagine it would make some code much easier to follow.

_pmf_2 replies#
> and the faster everyone can move on, the better!

Why? Where does this mindset come from?

nailer#
Direct style is easier to reason about. Control passes onto the next line consistently, for both async and sync operations.

Do you really prefer callbacks or just use them because they were once necessary? Are they so good you'd use them for sync operations too?

   add(a, b, function(sum){
     ....
   })
Personally: that seems like a waste of time. I'd rather just do:

  var sum = a + b;
Likewise I'd rather do:

  var response = await superagent.get('https://...')
scottmf#
Why would you want to use callbacks?

Here's a code example from an npm package I was looking at, vs how I would rewrite it in modern JS:
https://gist.github.com/scf4/8157abd755eb9c232e87dcc342b78b4...
Some of it may be preference but a lot of it is objectively simpler to reason about.
I don't understand why anyone would choose to make things unnecessarily complex.

fanpuns#
If OP wrote using ES2015 then it wouldn't be included, async is defined in 2017
baalimago1 reply#
Didn't know "var" was outdated. Good stuff, thanks!
scottmf2 replies#
You can technically still use var if you wish, but I'm not sure of any reasons to do so.

When switching to let/const, bear in mind the difference in scope:
var is function-scoped:

    (function () {
      var x = 1;
    })();

    var x === undefined
let/const are block-scoped:

    if (true) {
      var x = 1;
      let y = 1;
      const z = 1;
    }
    x === 1
    y === undefined
    z === undefined
mikekchar2 replies#
The only reason I can think of is if you want hoisting for some reason. For example, if you want to call functions before they are declared. Personally, I don't like that style, but some people do.
Cthulhu_3 replies#
let / const are both hoisted though, they're just hoisted to the top of the current block scope instead of function scope.

Continue this thread — 3 replies

scottmf#
Function declarations work like this

  typeof myFn === 'function'; // true
  function myFn() { }
z3t42 replies#
I find function lexical scope more natural for JavaScript which is very function orientated, where you pass functions around and use closures a lot. I also find it a PITA having to declare variables before the block, instead of where they are first used. Ex:

if(..) { ... huge block of code .. var foo = 1; }
Means I do not have to scroll back and forth to declare and set the variables at different places.

Cthulhu_#
That's probably because you're a more experienced Javascript developer and happen to know about it - and have experienced the pitfalls already. Most people, including myself (and I'm a fairly experienced JS developer myself) find block scoping to make more sense. Less prone to bugs and whatnot too.
rounce2 replies#
In strict mode JS runtimes will hoist the `var` declaration to the top of the declared scope anyway. So `foo` would be declared at the start of the containing function scope, regardless of where you appear to declare them.

Continue this thread — 2 replies

noir_lord#
This is incredible.

GF wants to learn to program, she has a strong math/finance background and I think she'd be good at programming but the web can be a bit overwhelming in totality.

oblib#
I went over a few chapters this afternoon and really enjoyed what you've put together. I'll definitely recommend it to friends who ask me about learning to code.

Load more comments