Home > Web Front-end > JS Tutorial > What are the characteristics of arrow functions in js

What are the characteristics of arrow functions in js

下次还敢
Release: 2024-05-06 13:39:19
Original
952 people have browsed it

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.

What are the characteristics of arrow functions in js

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:

  • Concise syntax: Arrow functions use an arrow => symbol instead of the traditional function keyword, the syntax is more concise.
// 传统函数
function add(a, b) {
  return a + b;
}

// 箭头函数
const add = (a, b) => a + b;
Copy after login
  • Implicit return: If the arrow function has only one expression, the curly braces and return keyword can be omitted. The arrow function will automatically return this expression.
// 传统函数
function square(x) {
  return x * x;
}

// 箭头函数
const square = x => x * x;
Copy after login
  • Lexical scope: Arrow functions inherit the 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.
const button = document.getElementById("my-button");

// 传统函数
button.addEventListener("click", function() {
  console.log(this); // 指向 button 元素
});

// 箭头函数
button.addEventListener("click", () => {
  console.log(this); // 指向 button 元素
});
Copy after login
  • Not constructible: Arrow functions cannot be used as constructors. This means that you cannot use the new keyword to create an instance of an arrow function.
  • Cannot declare arguments object: Arrow functions do not have their own arguments objects. The remainder operators ... are required to collect function parameters.
// 传统函数
function sum() {
  console.log(arguments); // 类似数组的对象
}

// 箭头函数
const sum = (...numbers) => {
  console.log(numbers); // 实际数组
};
Copy after login
  • Cannot use yield: Arrow functions cannot use the 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!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template