1

I came across the function below. There are multiple things I do not understand about the way it was written.

public getLegendCallback = (self => {
    function handle(chart) {
      return chart.legend.legendItems;
    }

    return chart => handle(chart);
})(this);
  1. Why does this seem to be a function inside a function? Couldn't you also write return chart.legend.legendItems to get the same outcome?

  2. What does the (this) at the end do?

  • 1
    [What is the (function() { } )() construct in JavaScript?](https://stackoverflow.com/q/8228281) | [What is the purpose of a self executing function in javascript?](https://stackoverflow.com/q/592396) | [How does this JavaScript/jQuery syntax work: (function( window, undefined ) { })(window)?](https://stackoverflow.com/q/2716069) | [Two sets of parentheses after function call](https://stackoverflow.com/q/18234491) – VLAZ Nov 13 '20 at 07:08
  • 2
    `this`/`self` doesn't appear to be used at all, and defining a separate `function handle` for that one-liner seems a bit superfluous too, especially when it's wrapped in yet another function on return. This could have been easily condensed to `public getLegendCallback(chart) { return chart.legend.legendItems; }`, as far as I can see. – deceze Nov 13 '20 at 07:10
  • 4
    It's a very convoluted way of writing `public getLegendCallback = (chart) => chart.legend.legendItems;` Is this auto-generated code? – JLRishe Nov 13 '20 at 07:12
  • Yes. Thanks to all of you! Now I'm able to understand what it does and can rewrite is as deceze and JLRishe suggested. – Severin Toldo Nov 17 '20 at 09:49

0 Answers0