" symbol instead of "function". Implicit return, braces and "return" can be omitted. Lexical scope, inherits the outer "this" value. Not constructible, cannot create an instance with "new". There is no "arguments" object, you need to use "..." to collect parameters. Cannot use "yield", not as a generator function."/> " symbol instead of "function". Implicit return, braces and "return" can be omitted. Lexical scope, inherits the outer "this" value. Not constructible, cannot create an instance with "new". There is no "arguments" object, you need to use "..." to collect parameters. Cannot use "yield", not as a generator function.">
Home > Article > Web Front-end > What are the characteristics of arrow functions in js
The features of JavaScript arrow functions include: concise syntax, using the "=>" symbol instead of "function". Implicit return, braces and "return" can be omitted. Lexical scope, inherits the outer "this" value. Not constructible, cannot create an instance with "new". There is no "arguments" object, you need to use "..." to collect parameters. Cannot use "yield", not as a generator function.
Characteristics of JavaScript arrow functions
The arrow function is a new syntax introduced in ES6, it is a short form function expression. Compared with traditional functions, arrow functions have the following characteristics:
=>
symbol instead of the traditional function
keyword, the syntax is more concise. <code class="js">// 传统函数 function add(a, b) { return a + b; } // 箭头函数 const add = (a, b) => a + b;</code>
return
keyword can be omitted. The arrow function will automatically return this expression. <code class="js">// 传统函数 function square(x) { return x * x; } // 箭头函数 const square = x => x * x;</code>
this
value of their outer scope instead of creating their own this
value. This makes arrow functions ideal for scenarios such as handling event handlers. <code class="js">const button = document.getElementById("my-button"); // 传统函数 button.addEventListener("click", function() { console.log(this); // 指向 button 元素 }); // 箭头函数 button.addEventListener("click", () => { console.log(this); // 指向 button 元素 });</code>
new
keyword to create an instance of an arrow function. arguments
objects. The remainder operators ...
are required to collect function parameters. <code class="js">// 传统函数 function sum() { console.log(arguments); // 类似数组的对象 } // 箭头函数 const sum = (...numbers) => { console.log(numbers); // 实际数组 };</code>
yield
keyword and therefore cannot be used as generator functions. The above is the detailed content of What are the characteristics of arrow functions in js. For more information, please follow other related articles on the PHP Chinese website!