Tag: ecmascript 6

Does ECMAScript 6 have a convention for abstract classes?

This question already has an answer here: How to create Abstract base class in JavaScript that can't be Instantiated 3 answers

Arrow function vs function declaration / expressions: Are they equivalent / exchangeable?

Canonical question If you find a question about issues after replacing a function declaration / expression with an arrow function, please close it as duplicate of this one. Arrow functions in ES2015 provide a more concise syntax. Can I replace all my function declarations / expressions with arrow functions now? What do I have to […]

Map vs Object in JavaScript

I just discovered chromestatus.com and, after losing several hours of my day, found this feature entry: Map: Map objects are simple key/value maps. That confused me. Regular JavaScript objects are dictionaries, so how is a Map different from a dictionary? Conceptually, they’re identical (according to What is the difference between a Map and a Dictionary?) […]

Spread operator vs Object.assign

Let’s say I have an options variable and I want to set some default value. What’s is the benefit / drawback of these two alternatives? Use the spread operator options = {…optionsDefault, …options}; Or use Object.assign options = Object.assign({}, optionsDefault, options); This is the commit that made me wonder.

Why es6 react component works only with “export default”?

This component does work: export class Template extends React.Component { render() { return ( <div> component </div> ); } }; export default Template; If i remove last row, it doesn’t work. Uncaught TypeError: Cannot read property ‘toUpperCase’ of undefined I guess, I don’t understand something in es6 syntax. Isn’t it have to export without sign […]

How do I write a named arrow function in ES2015?

I have a function that I am trying to convert to the new arrow syntax in ES6. It is a named function: function sayHello(name) { console.log(name + ‘ says hello’); } Is there a way to give it a name without: var sayHello = (name) => { console.log(name + ‘ says hello’); } Obviously, I […]

Declaring static constants in ES6 classes?

I want to implement constants in a class, because that’s where it makes sense to locate them in the code. So far, I have been implementing the following workaround with static methods: class MyClass { static constant1() { return 33; } static constant2() { return 2; } // … } I know there is a […]

Keyword 'const' does not make the value immutable. What does it mean?

There’s the const definition in Exploring ES6 by Dr. Axel Rauschmayer: const works like let, but the variable you declare must be immediately initialized, with a value that can’t be changed afterwards. […] const bar = 123; bar = 456; // TypeError: `bar` is read-only and then he writes Pitfall: const does not make the […]

How to add multiple classes to a ReactJS Component

I am new to ReactJS and JSX and I am having a little problem with the code below. I am trying to add multiple classes to the classNames attribute on each li: <li key={index} className={activeClass, data.class, “main-class”}></li> My React component is: var AccountMainMenu = React.createClass({ getInitialState: function(){ return { focused: 0 }; }, clicked: function(index){ […]

import modules from files in directory

With es6, I can import several exports from a file like this: import {ThingA, ThingB, ThingC} from ‘lib/things’; However, I like the organization of having one module per file. I end up with imports like this: import ThingA from ‘lib/things/ThingA’; import ThingB from ‘lib/things/ThingB’; import ThingC from ‘lib/things/ThingC’; I would love to be able to […]