ES6 arrow functions not working on the prototype?

Arrow functions provide a lexical this. It uses the this that is available at the time the function is evaluated. It is logically equivalent to (the following isn’t valid code since you can’t have a variable named this): (function(this){ // code that uses “this” })(this) In your 1st example the arrow function is within the … Read more

What are the differences (if any) between ES6 arrow functions and functions bound with Function.prototype.bind?

There are no (significant) differences. Well, okay, that’s a little premature. There are three tiny differences unique to arrow functions. Arrow functions cannot be used with new. This means, of course, that they do not have a prototype property and cannot be used to create an object with the classically-inspired syntax. new (() => {}) … Read more

JavaScript wait for asynchronous function in if statement [duplicate]

I do this exact thing using async/await in my games code here Assuming req.isLoggedIn() returns a boolean, it’s as simple as: const isLoggedIn = await req.isLoggedIn(); if (isLoggedIn) { // do login stuff } Or shorthand it to: if (await req.isLoggedIn()) { // do stuff } Make sure you have that inside an async function … Read more

require(‘babel/register’) doesn’t work

Since Babel 6 use babel-register hook to make on-the-fly transpilation. First: npm install babel-register Then require it with: require(‘babel-register’); // not using // require(‘babel/register’); // or // require(‘babel-core/register); To Convert your Ecmascript 6 code to ecmascript 5, you must set Babel presets option with require babel-register Like this: require(‘babel-register’)({ presets: [ ‘es2015’ ] }); Unlike … Read more

What is the difference between ‘let’ and ‘const’ ECMAScript 2015 (ES6)?

The difference between let and const is that once you bind a value/object to a variable using const, you can’t reassign to that variable. In other words Example: const something = {}; something = 10; // Error. let somethingElse = {}; somethingElse = 1000; // This is fine. The question details claim that this is … Read more

Why does `obj.foo = function() { };` not assign the name `foo` to the function?

Allen Wirfs-Brock has replied on the es-discuss list with the objections that prevented the TC39 consensus on the obj.foo = function() { } form: …for cache[getUserSecret(user)] = function() {}; it would leak the secret user info as the value of name and for obj[someSymbol] = function() {} it would leak the Symbol value as the … Read more

Why does the Airbnb style guide say that relying on function name inference is discouraged?

I think this could also have something to do with the unexpected behaviour that you might run into from implicitly giving a lexical name to what you may expect to be an anonymous function. Say for example someone understood the arrow function: (x) => x+2; To have the regular function equivalent: function(x) { return x+2; … Read more