Skip to content

Tag: ecmascript-6

ECMAScript 6 class destructor

I know ECMAScript 6 has constructors but is there such a thing as destructors for ECMAScript 6? For example if I register some of my object’s methods as event listeners in the constructor, I want to remove them when my object is deleted. One solution is to have a convention of creating a destructor meth…

How do you JSON.stringify an ES6 Map?

I’d like to start using ES6 Map instead of JS objects but I’m being held back because I can’t figure out how to JSON.stringify() a Map. My keys are guaranteed to be strings and my values will always be listed. Do I really have to write a wrapper method to serialize? Answer Both JSON.stringif…

Transforming a Javascript iterable into an array

I’m trying to use the new Map object from Javascript EC6, since it’s already supported in the latest Firefox and Chrome versions. But I’m finding it very limited in “functional” programming, because it lacks classic map, filter etc. methods that would work nicely with a [key, val…

How to feature-detect es6 modules

I’d like to export a module using the ES6 syntax only if it is supported in the current runtime. The goal is to develop a library that support different module management systems. As export is a keyword, something like will throw a syntax error : Answer Use ref

Object destructuring without var, let or const

Why does object destructuring throw an error if there is no var keyword in front of it? throws SyntaxError: expected expression, got ‘=’ The following three examples work without problems Bonus question: Why do we not need a var for array destructuring? I ran into the problem doing something like …