Hacker Timesnew | past | comments | ask | show | jobs | submitlogin

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


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


This is useful and works with functions that don't use `this` (like arrow functions).


`(a.c)()` is not undefined, it's 42


Nice catch, I should edit that but it's too late. I think my point still stands, however...




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: