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 method for every class that needs
Tag: ecmascript-6
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.stringify and JSON.parse support a second argument.
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, value] pair. It has a forEach but that does NOT returns the callback result.
How do I make a “public static field” in an ES6 class?
I’m making a Javascript class and I’d like to have a public static field like in Java. This is the relevant code: This is the error I get: It looks like ES6 modules don’t allow this. Is there a way to get the desired behavior or do I have to write a getter? Answer You make “public static field” using
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
Can I use ES6’s arrow function syntax with generators? (arrow notation)
That is, how do I express with arrow syntax? I’ve tried all the combinations I could think of, and I can’t find any documentation on it. (I am currently using Node.js v0.11.14.) Answer Can I use ES6’s arrow function syntax with generators? You can’t. Sorry. According to MDN The function* statement (function keyword followed by an asterisk) defines a generator
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 Answer The issue stems from the {…} operators having multiple
Javascript object bracket notation ({ Navigation } =) on left side of assign
I haven’t seen this syntax before and am wondering what it’s all about. The brackets on the left are throwing a syntax error: unexpected token { I’m not sure what part of the webpack config is transforming or what the purpose of the syntax is. Is it a Harmony thing? Can someone enlighten me? Answer It’s called destructuring assignment and
One-liner to take some properties from object in ES 6
How one can write a function, which takes only few attributes in most-compact way in ES6? I’ve came up with solution using destructuring + simplified object literal, but I don’t like that list of fields is repeated in the code. Is there an even slimmer solution? Answer Here’s something slimmer, although it doesn’t avoid repeating the list of fields. It
Why couldn’t popular JavaScript runtimes handle synchronous-looking asynchronous script?
As cowboy says down in the comments here, we all want to “write [non-blocking JavaScript] asynchronous code in a style similar to this: ” So people have come up solutions to this problem like callback libraries (eg async) promises event patterns streamline domains and generators. But none of these lead to code as simple and easy to understand as the