javascript-tutorial / javascript-tutorial/zh.javascript.info

[Suggestion] 建议在“构造函数和 "new" 运算符”章节末尾增加与现代 ES6 Class 的对比/过渡说明

Open Beginner friendly
#1,275 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
10.8k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

我在学习“[构造函数和 'new' 运算符](https://zh.javascript.info/constructor-new)”这一章节时,在现代编辑器(如 Zed)中尝试编写了页面中的示例代码:

```javascript
function User(name) {
this.name = name;
this.isAdmin = false;
}
let user = new User("Jack");

```

此时编辑器(内置的 TypeScript/JavaScript 语言服务)会弹出一个非常显眼的重构提示:

> `"This constructor function may be converted to a class declaration."`

对于初学者来说,看到这个提示可能会产生困惑(“既然教程教我用构造函数,为什么现代工具极力推荐我换成 class?”)。

### Suggested Improvement

为了让教程更具现代前瞻性,并完美衔接后续的 “Class” 章节,建议在本章末尾(例如在“总结”之前)增加一个**小提示(Notice/Sidebar)**,简要说明:

1. **为什么现代工具会给出这个提示:** 解释这是因为 ES6 引入了 `class`,它是构造函数的更安全的现代语法糖。
2. **核心区别(防错机制):** 简述构造函数漏写 `new` 会隐式失败(或污染全局),而 `class` 漏写 `new` 会直接抛出 `TypeError`,这也是 Linter/IDE 推荐重构的核心原因。
3. **引导:** 告诉读者现在可以继续学习构造函数以理解底层,后面会有专门的章节介绍 `class`。

这样可以及时解答读者在实际敲代码时遇到的编辑器警告,让教程与现代开发工具的反馈更加契合。

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the “Constructors and the new operator” chapter at https://zh.javascript.info/constructor-new and locate its source and summary section. Add a brief notice addressing the editor warning, the missing-new difference between constructor functions and classes, and the later Class chapter; done when the guidance is accurate, clear, and consistent with the tutorial’s existing style.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.