Home  >  Article  >  Web Front-end  >  Can Arrow Functions Create Their Own Arguments Object in ES6?

Can Arrow Functions Create Their Own Arguments Object in ES6?

Susan Sarandon
Susan SarandonOriginal
2024-10-18 15:54:03402browse

Can Arrow Functions Create Their Own Arguments Object in ES6?

ES6 Arrow Functions: Clarification on arguments Usage

Regarding the usage of arguments in ES6 arrow functions, confusion has arisen due to conflicting implementations between major browsers and Babel. This article seeks to clarify the official stance on this matter.

The Behavior Anomaly

In arrow functions, arguments should inherit their value from the parent scope. However, browsers like Chrome, Firefox, and Node violate this principle, allowing arrow functions to create their own arguments object.

Official Documentation

The official ECMA-262 specification (ES6) explicitly states that "specified statements, arrow functions do not have an own arguments binding in their scope; no arguments object is created when calling them." This implies that arrow functions should solely rely on the parent scope for accessing arguments.

Browser Implementations

Despite the official specification, Chrome, Firefox, and Node have opted to disregard this rule, while Babel adheres to it. This leads to the following discrepancy:

  • Browsers: arguments is available within arrow functions, deriving from their own scope.
  • Babel: arguments is unavailable within arrow functions, resulting in an "arguments is not defined" error.

Correct Usage

To avoid confusion and ensure compatibility across different environments, it's crucial to follow the official specification. Arrow functions should not have their own arguments object and any references to arguments within them should refer to the parent scope.

The above is the detailed content of Can Arrow Functions Create Their Own Arguments Object in ES6?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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