I have code like this I want to look at all of the classes in my application’s universe, and when I find one that descends from Animal, I want to create a new object of that type and add it to the list. This allows me to add functionality without having to update a list of things. So I can
Tag: ecmascript-6
Using Node.js require vs. ES6 import/export
In a project I am collaborating on, we have two choices on which module system we can use: Importing modules using require, and exporting using module.exports and exports.foo. Importing modules using ES6 import, and exporting using ES6 export Are there any performance benefits to using one over the other? Is there anything else that we should know if we were
How to unset a Javascript Constant in ES6?
I read this post, using delete keyword, we can delete JavaScript variable. But when I tried the same operations with constant but it is returning false when I try to delete constant. Is there any way to delete constants from memory? I tried this answer but its also not working. Answer You can’t directly do it, looking at the specs
JSON stringify a Set
How would one JSON.stringify() a Set? Things that did not work in Chromium 43: I would expect to get something similar to that of a serialized array. Answer JSON.stringify doesn’t directly work with sets because the data stored in the set is not stored as properties. But you can convert the set to an array. Then you will be able
Is it possible to sort a ES6 map object?
Is it possible to sort the entries of a es6 map object? results in: Is it possible to sort the entries based on their keys? Answer According MDN documentation: A Map object iterates its elements in insertion order. You could do it this way: Using .sort(), remember that the array is sorted according to each character’s Unicode code point value,
ECMAScript 6 spread syntax in object deconstruction. Support in TypeScript and Babel
Is the following valid ECMAScript 6? It seems to be supported by the latest version of Babel but it isn’t by TypeScript. I couldn’t find any ES6 references dealing with this case. Answer No, this is not valid ECMAScript 6. ES6 does only support rest syntax in function parameters and array destructuring, and spread syntax in function calls and array
Methods in ES6 objects: using arrow functions
In ES6, both of these are legal: and, as shorthand: Is it possible to use the new arrow functions as well? In trying something like or I get an error message suggesting that the method does not have access to this. Is this just a syntax issue, or can you not use fat-arrow methods inside of ES6 objects? Answer Arrow
.map() a Javascript ES6 Map?
How would you do this? Instinctively, I want to do: I’ve haven’t gleaned much from the documentation on the new iteration protocol. I am aware of wu.js, but I’m running a Babel project and don’t want to include Traceur, which it seems like it currently depends on. I also am a bit clueless as to how to extract how fitzgen/wu.js
ECMAScript 6: what is WeakSet for?
The WeakSet is supposed to store elements by weak reference. That is, if an object is not referenced by anything else, it should be cleaned from the WeakSet. I have written the following test: Even though my [1, 2, 3] array is not referenced by anything, it’s not being removed from the WeakSet. The console prints: Why is that? Plus,
Does ES6 introduce a well-defined order of enumeration for object properties?
Does ES6 introduce a well-defined order of enumeration for object properties? Answer Note: As of ES2020, even older operations like for-in and Object.keys are required to follow property order. That doesn’t change the fact that using property order for fundamental program logic probably isn’t a good idea, since the order for non-integer-index properties depends on when the properties were created.