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

How to make an iterator out of an ES6 class

You need to specify Symbol.iterator property for SomeClass which returns iterator for class instances. Iterator must have next() method, witch in turn returns object with done and value fields. Simplified example: function SomeClass() { this._data = [1,2,3,4]; } SomeClass.prototype[Symbol.iterator] = function() { var index = 0; var data = this._data; return { next: function() { … Read more

Methods in ES6 objects: using arrow functions

Arrow functions are not designed to be used in every situation merely as a shorter version of old-fashioned functions. They are not intended to replace function syntax using the function keyword. The most common use case for arrow functions is as short “lambdas” which do not redefine this, often used when passing a function as … Read more

JavaScript double colon (bind operator)

No. The bind operator (spec proposal) comes in two flavours: Method extraction ::obj.method ≡ obj.method.bind(obj) “virtual method” calls obj::function ≡ function.bind(obj) obj::function(…) ≡ function.call(obj, …) Neither of them feature partial application. For what you want, you should use an arrow function: (…args) => this.handleStuff(‘stuff’, …args) ≡ this.handleStuff.bind(this, ‘stuff’)

One-liner to take some properties from object in ES 6

Here’s something slimmer, although it doesn’t avoid repeating the list of fields. It uses “parameter destructuring” to avoid the need for the v parameter. ({id, title}) => ({id, title}) (See a runnable example in this other answer). @EthanBrown’s solution is more general. Here is a more idiomatic version of it which uses Object.assign, and computed … Read more

Is there a mechanism to loop x times in ES6 (ECMAScript 6) without mutable variables?

Using the ES2015 Spread operator: […Array(n)].map() const res = […Array(10)].map((_, i) => { return i * 10; }); // as a one liner const res = […Array(10)].map((_, i) => i * 10); Or if you don’t need the result: […Array(10)].forEach((_, i) => { console.log(i); }); // as a one liner […Array(10)].forEach((_, i) => console.log(i)); Or … Read more

How to customize object equality for JavaScript Set

Update 3/2022 There is currently a proposal to add Records and Tuples (basically immutable Objects and Arrays) to Javascript. In that proposal, it offers direct comparison of Records and Tuples using === or !== where it compares values, not just object references AND relevant to this answer both Set and Map objects would use the … Read more

What is the motivation for bringing Symbols to ES6?

The original motivation for introducing symbols to Javascript was to enable private properties. Unfortunately, they ended up being severely downgraded. They are no longer private, since you can find them via reflection, for example, using Object.getOwnPropertySymbols or proxies. They are now known as unique symbols and their only intended use is to avoid name clashes … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)