Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: modifying the name of a named function expression results in TypeError only in strict mode #36803

Merged
merged 3 commits into from
Nov 16, 2024

Conversation

liuxuelian
Copy link
Contributor

… strict mode

Description

In the "Named function expression", the name of the function expression is read-only. However, the name of the function is modified inside the function body and does not cause an error in non-strict mode. However, it only causes TypeError in strict mode.

Therefore, the corrective this code needs to put a "use strict"; statement before any other statements to be correct, as demonstrated here:

"use strict";

function foo() {
  foo = 1;
}
foo();
console.log(foo); // 1
(function foo() {
  foo = 1; // TypeError: Assignment to constant variable.
})();

In Google Chrome, the result is as follows: (other browsers are similar)

image

Motivation

Additional details

Related issues and pull requests

@liuxuelian liuxuelian requested a review from a team as a code owner November 15, 2024 06:08
@liuxuelian liuxuelian requested review from Josh-Cena and removed request for a team November 15, 2024 06:08
@github-actions github-actions bot added Content:JS JavaScript docs size/xs [PR only] 0-5 LoC changed labels Nov 15, 2024
Copy link
Contributor

github-actions bot commented Nov 15, 2024

Preview URLs

(comment last updated: 2024-11-15 14:18:38)

@Josh-Cena Josh-Cena merged commit 0365c8e into mdn:main Nov 16, 2024
8 checks passed
@liuxuelian liuxuelian deleted the patch-3 branch November 16, 2024 03:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:JS JavaScript docs size/xs [PR only] 0-5 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants