What’s the purpose of an asterisk (*) in ES6 generator functions

The three reasons were: Readability. A generator is quite different from a function, and the difference should be immediately visible (that is, without examining the whole implementation in search for a yield). Generality. It should be naturally possible to write generators that do not yield, and only return directly. Moreover, commenting out part of the … Read more

jest.mock(): How to mock ES6 class default import using factory parameter

Updated with a solution thanks to feedback from @SimenB on GitHub. Factory function must return a function The factory function must return the mock: the object that takes the place of whatever it’s mocking. Since we’re mocking an ES6 class, which is a function with some syntactic sugar, then the mock must itself be a … Read more

Why doesn’t JavaScript ES6 support multi-constructor classes?

I want to write my Javascript class like below You can’t, in the same way you can’t overload standard functions like that. What you can do is use the arguments object to query the number of arguments passed: class Option { constructor(key, value, autoLoad) { // new Option() if(!arguments.length) { this.autoLoad = false; } // … Read more

Use reselect selector with parameters

Updated: 16 February 2022 New Solution from Reselect 4.1: See detail // selector.js const selectItemsByCategory = createSelector( [ // Usual first input – extract value from `state` state => state.items, // Take the second arg, `category`, and forward to the output selector (state, category) => category ], // Output selector gets (`items, category)` as args … Read more