Skip to content

Instantly share code, notes, and snippets.

@shaun-stripe
Last active March 31, 2016 21:58
Show Gist options
  • Select an option

  • Save shaun-stripe/7cc86ad50893e4427105 to your computer and use it in GitHub Desktop.

Select an option

Save shaun-stripe/7cc86ad50893e4427105 to your computer and use it in GitHub Desktop.
JS gotchas

Shorthand anonymous functions

JS allows you to create shorthand anonymous functions, but it gets confused if you're trying to return an Object.

(a,b) => a+b
(a,b) => {sum: a+b}  // cannot return an Object like this, because curlies mean body
(a,b) => { let sum = a+b; return sum; }

Constants are mutable

JS constants prevent reassignment, but allow inner mutation. (i.e. like constant pointers to non-constant values in C).

const a = [];
a.push(1); // allowed
a = [];    // not allowed

Circular Dependency Traps

// inside app/main/index.js
import Foo from 'app/main/foo';
import Bar from 'app/main/bar';

export default {
  Foo,
  Bar,
};
// inside app/main/foo.js
import {Bar} from 'app/main';   // Circular Dependency will silently null all of `app/main`
import Bar from 'app/main/bar'; // <--- workaround that won't fail

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