Wildcard or asterisk (*) vs named or selective import es6 javascript

If you use webpack with the dead code elimination provided by the new uglify, or rollupjs with tree-shaking, then the unused imports will be stripped. I partially agree with the airbnb styleguide to not to use wildcard imports, although javascripts wildcard imports do not suffer from the same diseases as for example pythons or javas … Read more

What’s the purpose of the HTML “nomodule” attribute for script elements if the default is text/javascript?

The purpose of the nomodule attribute is to cause newer browsers that support module scripts to ignore a particular script element: The nomodule attribute is a boolean attribute that prevents a script from being executed in user agents that support module scripts. The spec has a good example: This example shows how to include a … Read more

ES6 import equivalent of require() without exports

The equivalent is simply: import “./filename”; Here are some of the possible syntax variations: import defaultMember from “module-name”; import * as name from “module-name”; import { member } from “module-name”; import { member as alias } from “module-name”; import { member1 , member2 } from “module-name”; import { member1 , member2 as alias2 , […] … Read more

Destructuring in Node.JS

Update for node v6 and newer: Node v6 supports destructuring assignment without anything special needed: var [a, b] = [1, 2]; For older versions of node: You can get the list of supported harmony features by typing: node –v8-options | grep harmony node 5.x will give you: –es_staging (enable all completed harmony features) –harmony (enable … Read more

Naming convention for const object keys in ES6

NOTE: be aware that the accepted response has a link to an obsolete Google style guide This is nice (string literals or integer literals): const PI = 3.14; const ADDRESS = ‘10.0.0.1’; but… const myObject = { key: ‘value’ }; const userSuppliedNumber = getInputNumber() Google JavaScript Style Guide says: Declare all local variables with either … Read more

Browserify, Babel 6, Gulp – Unexpected token on spread operator

That syntax is an experimental proposed syntax for the future, it is not part of es2015 or react so you’ll need to enable it. npm install –save-dev babel-plugin-transform-object-rest-spread and add “plugins”: [“transform-object-rest-spread”] into .babelrc alongside your existing presets. Alternatively: npm install –save-dev babel-preset-stage-3 and use stage-3 in your presets to enable all stage-3 experimental functionality.

Converting Object to Array using ES6 features

Use (ES5) Array::map over the keys with an arrow function (for short syntax only, not functionality): let arr = Object.keys(obj).map((k) => obj[k]) True ES6 style would be to write a generator, and convert that iterable into an array: function* values(obj) { for (let prop of Object.keys(obj)) // own properties, you might use // for (let … Read more

React.js ES6 avoid binding ‘this’ to every method

You can use class fields to do the binding outside the constructor. They look like the following: class Foo extends React.Component { handleBar = () => { console.log(‘neat’); }; handleFoo = () => { console.log(‘cool’); }; render() { return ( <div onClick={this.handleBar} onMouseOver={this.handleFoo} /> ); } } Class fields are supported experimentally by Babel via … Read more