Skip to content

Instantly share code, notes, and snippets.

@royling
Last active May 9, 2016 07:16
Show Gist options
  • Save royling/959420746e1307e6bb4e0685ac1081a7 to your computer and use it in GitHub Desktop.
Save royling/959420746e1307e6bb4e0685ac1081a7 to your computer and use it in GitHub Desktop.
`(0, mod.fn)(args)` in babel transpiled code

Found an interesting snippet in the babel transpiled code.

For the below ES6 code snippet:

import { speak } from './mod_test';
speak('world');

Babel will transpile it to ES5 as below:

(0, _mod_test.speak)('world');

It is to convert a method call to a regular function call which is what intented in ES6 syntax. The equivalent code with explicit assignment expression:

var _speak = _mod_test.speak;
_speak('world');

Without the assignment, this in the call refers to the module instance _mod_test instead of global (ie. window/global).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment