News for hackers. Stuff that matters.
Re:Comparing Svelte and React by framecowbird on Wednesday March 24, 2021 @04:09PM (#26572269)
Javascript n00b question... is there any difference between

onMount(() => { return listenForAuthChanges() })

and

onMount(() => listenForAuthChanges())

?

Re:Comparing Svelte and React by jfengel on Wednesday March 24, 2021 @04:24PM (#26572414)
They are the same. It's a matter of style.

For that matter, you could also write:

onMount(listenForAuthChanges)

which is technically simpler -- you're not constructing a new anonymous function. But if you wanted to extend the code (to say, add a console.log), you'd need to do the top version, which already has the braces.

It's up to your judgment of what's clearer and likely to be more maintainable.

Re:Comparing Svelte and React by M2Ys4U on Wednesday March 24, 2021 @05:23PM (#26573114)
That may be visually simpler, but if onMount were to start passing a parameter to its callback in a future version (for whatever reason) then that will be passed to listenForAuthChanges. Using the anonymous function means that wouldn't happen.
Re:Comparing Svelte and React by brundolf on Wednesday March 24, 2021 @04:54PM (#26572771)
This is a utility function I carry around between projects for the "logging within a pure expression" case:

  function log(expr) {
    console.log(expr);
    return expr;
  }

  onMount(() => log(listenForAuthChanges()))
Re:Comparing Svelte and React by hoten on Wednesday March 24, 2021 @05:53PM (#26573459)
That should work in the provided example, but in general `fn(cb)` is different: you lose the implied `this` binding unless you do `cb.bind(something)` beforehand.

There's also some unexpected trouble due to potential extra parameters. Consider the output of

  ['1', '2', '3'].map(parseInt)

gives:

  [1, NaN, NaN]
So yea, fn(() => cb()) is the same as fn(() => {return cb()}) – just syntactic sugar when the anonymous function has just one statement. but fn(cb) is semantically different.
Re:Comparing Svelte and React by jfengel on Thursday March 25, 2021 @09:18AM (#26579879)
I consider myself a pretty advanced JS developer, and I still don't understand how "this" works in JS. I avoid using it whenever I can.
Re:Comparing Svelte and React by hoten on Friday March 26, 2021 @06:11PM (#26597612)
Yea, you can avoid it without losing much at all.

Most common place IME you end up needing to use it are when assigning functions on a class as event listeners: if you want to be able to remove them, you need to use the same value in `addEventListener` as `removeEventListener`; and if the function uses other properties of the class, you must use `.bind(this)` to correctly set `this`.

It may help to think of it like this. This code

  object.someFunction(a, b);
is syntactic sugar for

  someFunction.call(object, a, b)
So with most usages of functions, the `this` is implicitly defined. The trouble comes when you start passing around references to functions.

  const fn = object.someFunction;
  fn(a, b); // throw `this is undefined` error if function accesses this
Here, `this` was never defined. So you need to do one of:

  const fn = object.someFunction.bind(object);
  fn(a ,b);
  // OR
  fn.call(object, a, b);
The last thing you need to know is the difference between arrow functions and classic functions. Arrow functions _never_ have a `this` binding of its own. It doesn't make a scope. If the scope it was defined in happened to already define `this`, it would use that. Classic functions (`function blah() {}` or `const fn = function() {}`) do make a new scope, so they have a `this` binding, so if you wanted to access `this` of the outer scope you'd have to assign it to another variable beforehand

  class A {
    a = 1;
    fn() {
      const thisRef = this;
      return function () { return thisRef.a }
    }
  }
With arrow functions, it's easier:

  class A {
    a = 1;
    fn() {
      return () => { return this.a }
    }
  }

I think this is all you'd ever need to know.
Re:Comparing Svelte and React by filoleg on Wednesday March 24, 2021 @06:37PM (#26573904)
>For that matter, you could also write:

>onMount(listenForAuthChanges)

>which is technically simpler -- you're not constructing a new anonymous function.

Not just technically simpler, it is a perf improvement over initializing an anonymous function every time onMount is called (which you have already mentioned, I just wanted to emphasize). A minor performance improvement at that, but still, small things add up. I agree with the entirety of your post, however, well-written.

Re:Comparing Svelte and React by oplav on Wednesday March 24, 2021 @04:10PM (#26572285)
No, they are functionally the same.
Re:Comparing Svelte and React by hajile on Thursday March 25, 2021 @02:17PM (#26583555)
No. They are identical
Re:Comparing Svelte and React by KitDuncan on Wednesday March 24, 2021 @04:51PM (#26572716)
no it's the same
Re:Comparing Svelte and React by hazzahzah on Wednesday March 24, 2021 @05:22PM (#26573104)
same
Re:Comparing Svelte and React by rahulpadalkar on Wednesday March 24, 2021 @04:11PM (#26572292)
no