How do I write a named arrow function in ES2015?
How do I write a named arrow function in ES2015?
You do it the way you ruled out in your question: You put it on the right-hand side of an assignment or property initializer where the variable or property name can reasonably be used as a name by the JavaScript engine. There's no other way to do it, but doing that is correct and fully covered by the specification. (It also works for traditional anonymous function
expressions.)
Per spec, this function has a true name, sayHello
:
const sayHello = (name) => {
console.log(name + ' says hello');
};
console.log(sayHello.name); // "sayHello"
What's the correct syntax for a named arrow function registered as callback?
If you move the function definition before the line where it's used, it should work. Like this:
http.createServer((req, res) => {
const renderIndex = (err, data) => {
if (err) throw err;
res.writeHead(200, ct);
console.log("Rendering index");
return res.end(data);
}
let q = url.parse(req.url, true);
if (q.pathname === "/") {
fs.readFile('./index.html', renderIndex);
}
}).listen(8080)
If it were my code, I would move the function definition outside of the containing function, completely, currying it so I can pass res
to it before it's used as a callback:
const renderIndex = res => (err, data) => {
if (err) throw err;
res.writeHead(200, ct);
console.log("Rendering index");
return res.end(data);
}
http.createServer((req, res) => {
let q = url.parse(req.url, true);
if (q.pathname === "/") {
fs.readFile('./index.html', renderIndex(res));
}
}).listen(8080)
Is there a way to name arrow functions in JavaScript?
Is there a way to name arrow functions so that a reference can be used from within?
Not unless you assign it to a variable. For example:
var foo = () => {
console.log(foo);
}
For arrow functions, I'm currently using arguments.callee
arguments
are not supported by arrow functions. TypeScript currently incorrectly allows you to use them. This will be an error in the next version of TypeScript. This is to keep TypeScript arrow functions compatible with the JavaScript Language Specification.
For your use case I would just use a function
.
How do you create a named async arrow function?
Arrow functions have no name, but you can assign them to a variable like this:
const normalFunc = () => { ... };
const asyncFunc = async () => { ... };
Do note, however, that arrow functions are not just shorter notation for regular functions, as there are some subtle differences to be aware of (see this article for details). However, if you understand these differences and they don't affect your code, you should be fine.
Transpile Arrow Functions into Named Functions without Variable Assignment
Based on the comments above the answer seems to be, "You should not want to do this."
Babel transforms arrow functions as illustrated above in order to avoid potential unintended side effects in production.
Related Topics
Working with Select Using Angularjs's Ng-Options
How to Access the First Property of a JavaScript Object
When to Use Setattribute VS .Attribute= in JavaScript
How to Call a JavaScript Function on Page Load
Open a New Tab in the Background
Jquery Click/Toggle Between Two Functions
How to Synchronize a Sequence of Promises
Getting Around X-Frame-Options Deny in a Chrome Extension
JavaScript Setinterval and 'This' Solution
Promise.All Consumes All My Ram
Why JavaScript Treats a Number as Octal If It Has a Leading Zero
Detect Ie Version (Prior to V9) in JavaScript
Insert HTML at Caret in a Contenteditable Div
How to Quickly and Conveniently Disable All Console.Log Statements in My Code