Recently I've seen some code example with javascript class, when declaring an async method, it does not contain the function
keyword, nor it's using an arrow function as usual, code example
export default class CartClient {
async getCart(authToken, cartId) {
const request = this.request
.url(`${this.url}/${cartId}`)
.get()
.auth(authToken)
.withNoCache()
.build();
const response = await fetch(request);
return await response.json();
}
}
This is the very first time I've seen things like this, usually I was told to create function using function
keyword or arrow function like below
async function getCart() {
// implementation
}
or
async getCart = () => {
// implementation
}
Could anyone let me know why we don't need the function
keyword anymore when declaring the method? Is it a javascript related feature or react feature? Since I've seen in a react project.
Methods in classes do not have the function
prefix. In fact, they are disallowed.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.