1

Code in the wild:

return function decorateSource(DecoratedComponent) {
    return (0, _decorateHandler2.default)({
        connectBackend: function connectBackend(backend, sourceId) {
            return backend.connectDragSource(sourceId);
        },
        containerDisplayName: 'DragSource',
        createHandler: createSource,
        registerHandler: _registerSource2.default,
        createMonitor: _createSourceMonitor2.default,
        createConnector: _createSourceConnector2.default,
        DecoratedComponent: DecoratedComponent,
        getType: getType,
        collect: collect,
        options: options
    });
};

And the construct in question:

(0, _decorateHandler2.default)(...)

What is going on with this wrapped statement?

CNSKnight
  • 567
  • 7
  • 14
  • Maybe related http://stackoverflow.com/questions/9107240/1-evalthis-vs-evalthis-in-javascript/9107367#9107367 – elclanrs Mar 10 '17 at 17:57

1 Answers1

0

It's similar to doing this:

(function(){
    // do something
})();

In that because the element is wrapped in parentheses gets treated as a value, and the last value in the parentheses is being executed as a function by the following parentheses. The 0, isn't doing anything. Removing 0, won't change how the code is run, so in this case I figure 0, is just obfuscation as whatever _decorateHandler2.default returns is what decorateSource returns. The benefit of doing this (or the code sample above) is that you're creating a closure that help you control the scope of your variables and not pollute the global scope.

winhowes
  • 7,845
  • 5
  • 28
  • 39