JavaScript function: Arrow functions: Trailing comma in parameters. If you use a normal function as opposed toĪn arrow function, this will not be an instance of M圜lass. Function shorthand using > syntax and lexical this binding. this in the arrow function is the same as this outside the arrow function.įor example, suppose you try to call setTimeout() in aĬlass method.
#Arrow function javascript code#
Arrow function expressions are ill-suited as methods, and they cannot be used as constructors. If your arrow function takes 1 param, you can omit the // parentheses around the parameter names let noop = v => v According to MDN, An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords. If your arrow function takes no params, declare it with // `() =>` const getAnswer = () => 42
Using an arrow function is the wrong choice.
However, you still need to learn when to useĬonventional functions versus when to use arrow functions, because Were introduced in ES6 to work around several common gotchas withĬonventional functions.