Home > Web Front-end > JS Tutorial > body text

When Do Arrow Functions or the Parent Scope Define the Arguments of a Function?

DDD
Release: 2024-10-18 16:00:05
Original
430 people have browsed it

When Do Arrow Functions or the Parent Scope Define the Arguments of a Function?

Arguments in ES6 Arrow Functions: Official Clarification

In ES6 arrow functions, the behavior of the arguments keyword has been a topic of debate. Some browsers and platforms (such as Chrome, Firefox, and Node) deviate from the initial TC39 recommendations, raising questions about the correct interpretation of the specification.

According to the official ES6 specification, arrow functions do not have their own arguments binding within their scope. When invoked, arrow functions do not create an arguments object as part of the declaration instantiation process.

This contradicts the behavior observed in browsers such as Chrome, Firefox, and Node, which create an arguments object in arrow functions. As a result, these browsers incorrectly provide access to the arguments passed to the enclosing function.

Babel, on the other hand, follows the official specification by throwing a "ReferenceError" when arguments is accessed inside an arrow function. This aligns with the intended behavior of arrow functions as functions that adopt their parent scope for arguments access.

Therefore, the correct understanding is that arrow functions do not have a dedicated arguments binding. Developers should use the arguments object provided by the enclosing function to access passed arguments when working with arrow functions.

The above is the detailed content of When Do Arrow Functions or the Parent Scope Define the Arguments of a Function?. For more information, please follow other related articles on the PHP Chinese website!

source:php
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