I keep hearing that arrow functions inherit the value of this from their Lexical Environment. Consider this example: Why is the value of this inside the arrow callback functions undefined (or in non-strict mode: window)? If the callback function is using the value of this from its lexical environment, shouldn’t the lexical environment be addEventListener? Answer When you call a
Tag: arrow-functions
Difference between two ways of resolving a promise
I would like to understand the difference between the two code snippets, both resolving promises in different ways but receiving the same result (resolving after a second). Which one is the right way? Answer In this case, where you’re not resolving with a value, there is no difference. In situations where you need to resolve the promise with a value,
MongoDB: $function operator does not support arrow function
I have following documents in testCollection: I am using regular expression to filter documents using $function operator. I get the correct output while using Query 1. Query 1: Output for Query 1: but for Query 2 I am getting all the documents and the filter is not working. In Query 2 I changed the function body to arrow function. Query
Fixing ‘consistent-return’ linter issue with simple arrow function
As title, I have an arrow function with the error. How can I refactor it so the error goes away? Expected to return a value at the end of arrow function consistent-return This is my code: Thanks! Answer By always explicitly returning a value under all code branches: Assuming this is eslint (but really regardless of the linter), there is
Using _ (underscore) variable with arrow functions in ES6/Typescript
I came across this construct in an Angular example and I wonder why this is chosen: I understand that the variable _ means don’t care/not used but since it is the only variable is there any reason to prefer the use of _ over: Surely this can’t be about one character less to type. The () syntax conveys the intent
Arrow function without curly braces
I’m new to both ES6 and React and I keep seeing arrow functions. Why is it that some arrow functions use curly braces after the fat arrow and some use parentheses? For example: vs. Answer The parenthesis are returning a single value, the curly braces are executing multiple lines of code. Your example looks confusing because it’s using JSX which
JSDoc: arrow function params
I’m trying to document my code with JSDoc (EcmaScript 2015, WebStorm 12 Build 144.3357.8). I have an arrow function which I want to document its parameters. This two examples work (I get auto-completion): But when I want to document an arrow function in forEach function, for example, the auto-completion isn’t working (all of the below): Has anyone managed to get
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