Is it possible to use ES6 modules in Mocha tests?

Mocha has support for ESM from version 7.1.0 onward (release: Feb. 26, 2020). This requires Node 12.11.0 or higher, and is subject to the current restrictions/limitations of using modules in Node: Either you must use .mjs file extensions for source files that use ES modules, or you must have “type”: “module” in your package.json You … Read more

ES6 – declare a prototype method on a class with an import statement

You can still attach a method on a class’ prototype; after-all, classes are just syntactic sugar over a “functional object”, which is the old way of using a function to construct objects. Since you want to use ES6, I’ll use an ES6 import. Minimal effort, using the prototype: import getColor from ‘path/to/module’; class Car { … Read more

Proper way to dynamically add functions to ES6 classes

Your solution is fine, though it’ll be better to create all those methods once on a prototype level: [‘GET’, ‘PUT’, ‘POST’, ‘DEL’].forEach((method) => { Executor.prototype[method] = function (body) { return this.request(method, body) } }) prototype approach is slightly faster, because this code is executed only once, while constructor code is executed every time new instance … Read more

Difference between Configurable and Writable attributes of an Object

From: http://ejohn.org/blog/ecmascript-5-objects-and-properties/ Writable: If false, the value of the property can not be changed. Configurable: If false, any attempts to delete the property or change its attributes (Writable, Configurable, or Enumerable) will fail. Enumerable: If true, the property will be iterated over when a user does for (var prop in obj){} (or similar).

What do the parentheses wrapping the object literal in an arrow function mean? [duplicate]

No. Those parentheses produce an object literal. Arrow functions have many syntaxes, one of which is: ( … ) => expression This will implicitly return an expression, for example: () => 1 + 1 This function will implicitly return 1 + 1, which is 2. Another one is this: ( … ) => { … … Read more