News for hackers. Stuff that matters.
Re:Javascript Fundamentals: ‘this’ keyword by anonytrary on Saturday August 25, 2018 @04:02PM (#17842328)
This example is needlessly confusing:

  const a = {
    b: 42,
    c: function() {
      return this.b;
    }
  };

  (a.c || [])(); // 1
  (a.c)(); // 2
  (1, a.c)(); // 3
He's trying to illustrate that an expression returns a reference to the (unbound) function. This line "(a.c || [])()" is bothering me -- why on earth would this line exist in any program? Are we trying to throw a type error on purpose? Maybe "(a.c || (()=>{}))()" would have been more appropriate.

I spent about 5 minutes wondering why each line returned 1, 2 and 3 respectively, when they should be returning the global value of b. If you are going to be showing obscure, tricky examples of Javascript, the last thing you want to do is have superfluous comments in your code. Follow comment conventions for the sake of your readers' sanity. This would have been much less confusing:

  const a = {
    b: 42,
    c: function() {
      return this.b;
    }
  };

  (a.c || (() => {}))(); // undefined
  (a.c)(); // undefined
  (1, a.c)(); // undefined
Re:Javascript Fundamentals: ‘this’ keyword by wruza on Sunday August 26, 2018 @03:20AM (#17844727)
>This line "(a.c || [])()" is bothering me -- why on earth would this line exist in any program?

A variation of it is valid for method selection, such as:

  (cond ? a.foo : b.bar)();
or, for method fallback:

  (a.foo || a.fooDefault)();
Neither does work in js, though.
Re:Javascript Fundamentals: ‘this’ keyword by anonytrary on Tuesday August 28, 2018 @03:52AM (#17857216)
This is useful and works with functions that don't use `this` (like arrow functions).
Re:Javascript Fundamentals: ‘this’ keyword by pitaj on Saturday August 25, 2018 @05:41PM (#17842719)
`(a.c)()` is not undefined, it's 42
Re:Javascript Fundamentals: ‘this’ keyword by anonytrary on Sunday August 26, 2018 @01:06AM (#17844372)
Nice catch, I should edit that but it's too late. I think my point still stands, however...