On 13/03/2018 01:42, RobG wrote:
> Since block scoped variables were introduced in ECMAScript 2015,
> it's been possible to implement the same "private variable" functionality as
> is available using the IIFE pattern.
> [snip]
You may replace an IIFE with a block and a let or const declaration
(*don't use 'var' in this case*) or a module in ES6. There were some
examples of that in the book 'Exploring ES6', section 12.3.5, by Dr.
Axel Rauschmayer:
<
http://exploringjs.com/es6/ch_callables.html#sec_iifes-in-es6>
There's also the possibility of using an Immediately-Invoked Arrow
Function (IIAF) in ES6. For example:
/*jshint esnext: true */
(() => {
// code.
})();
BTW, your getNext() function may be refactored to a Generator in ES6:
/*jshint esnext: true */
{
let gen = (function* idGen() {
let i = 0;
while(true) yield i++;
}());
console.log(gen.next()); // 0
console.log(gen.next()); // 1
console.log(gen.next()); // 3
}
--
Joao Rodrigues