ECMAScript 6 class destructor

Is there such a thing as destructors for ECMAScript 6? No. EcmaScript 6 does not specify any garbage collection semantics at all[1], so there is nothing like a “destruction” either. 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 A … Read more

Re-export default in ES 6 modules

If you use proposal-export-default-from Babel plugin (which is a part of stage-1 preset), you’ll be able to re-export default using the following code: export default from “./App.js” For more information see the ECMAScript proposal. Another way (without this plugin) is: export { default as App } from “./App.js” The above is a very common practice … 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 object bracket notation ({ Navigation } =) on left side of assign

It’s called destructuring assignment and it’s part of the ES2015 standard. The destructuring assignment syntax is a JavaScript expression that makes it possible to extract data from arrays or objects using a syntax that mirrors the construction of array and object literals. Source: Destructuring assignment reference on MDN Object destructuring var o = {p: 42, … Read more

Backticks (`…`) calling a function in JavaScript

It is called Tagged Template in ES-6 more could be read about them Here, funny I found the link in the starred section of the very chat. But the relevant part of the code is below (you can basically create a filtered sort). function tag(strings, …values) { assert(strings[0] === ‘a’); assert(strings[1] === ‘b’); assert(values[0] === … Read more

Why does [NaN].includes(NaN) return true in JavaScript?

According to MDN’s document say that Note: Technically speaking, includes() uses the sameValueZero algorithm to determine whether the given element is found. const x = NaN, y = NaN; console.log(x == y); // false -> using ‘loose’ equality console.log(x === y); // false -> using ‘strict’ equality console.log([x].indexOf(y)); // -1 (false) -> using ‘strict’ equality … Read more

Extending Error in Javascript with ES6 syntax & Babel

Based on Karel Bílek’s answer, I’d make a small change to the constructor: class ExtendableError extends Error { constructor(message) { super(message); this.name = this.constructor.name; if (typeof Error.captureStackTrace === ‘function’) { Error.captureStackTrace(this, this.constructor); } else { this.stack = (new Error(message)).stack; } } } // now I can extend class MyError extends ExtendableError {} var myerror = … Read more

Deep copy in ES6 using the spread syntax

Use JSON for deep copy var newObject = JSON.parse(JSON.stringify(oldObject)) var oldObject = { name: ‘A’, address: { street: ‘Station Road’, city: ‘Pune’ } } var newObject = JSON.parse(JSON.stringify(oldObject)); newObject.address.city = ‘Delhi’; console.log(‘newObject’); console.log(newObject); console.log(‘oldObject’); console.log(oldObject);

Using spread syntax and new Set() with typescript

Update: With Typescript 2.3, you can now add “downlevelIteration”: true to your tsconfig, and this will work while targeting ES5. The downside of downlevelIteration is that TS will have to inject quite a bit of boilerplate when transpiling. The single line from the question transpiles with 21 lines of added boilerplate: (as of Typescript 2.6.1) … Read more