Skip to content

[zh-cn]: Update the translation of extends in class section - #19869

Merged
yin1999 merged 45 commits into
mdn:mainfrom
tianyeeT:js-class-extends
May 7, 2024
Merged

yin1999 merged 45 commits into
mdn:mainfrom
tianyeeT:js-class-extends

Conversation

@tianyeeT

Copy link
Copy Markdown
Contributor

Description

Motivation

Additional details

Related issues and pull requests

extends part of #18329

@tianyeeT
tianyeeT requested review from a team and irvin and removed request for a team April 29, 2024 15:04
@github-actions github-actions Bot added the l10n-zh Issues related to Chinese content. label Apr 29, 2024
@github-actions

github-actions Bot commented Apr 29, 2024 •

Copy link
Copy Markdown
Contributor

Preview URLs

External URLs (5)

URL: /zh-CN/docs/Web/JavaScript/Reference/Classes/extends
Title: extends

(comment last updated: 2024-05-07 04:58:45)

tianyeeT and others added 11 commits April 29, 2024 23:08
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
tianyeeT and others added 7 commits April 30, 2024 08:37
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
tianyeeT and others added 3 commits April 30, 2024 08:41
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

许多内置复制方法都实现了这一行为。有关此功能的注意事项,请参阅 [subclassing built-ins](#subclassing_built-ins) 讨论。

### Mix-ins

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Mix-ins
### 混入

本章节的其他mix-in也做一下翻译

tianyeeT and others added 12 commits May 5, 2024 20:07
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Comment on lines +61 to +63
| | `ChildClass` 的原型对象 | `ChildClass.prototype` 的原型对象 |
| --------------------------------- | ----------------------- | --------------------------------- |
| `extends` clause absent | `Function.prototype` | `Object.prototype` |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[mdn-linter] reported by reviewdog 🐶

Suggested change
| | `ChildClass` 的原型对象 | `ChildClass.prototype` 的原型对象 |
| --------------------------------- | ----------------------- | --------------------------------- |
| `extends` clause absent | `Function.prototype` | `Object.prototype` |
| | `ChildClass` 的原型对象 | `ChildClass.prototype` 的原型对象 |
| ---------------------------- | ----------------------- | --------------------------------- |
| `extends` clause absent | `Function.prototype` | `Object.prototype` |

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不应直接忽略,我们使用格式化工具格式化 markdown 表格。

tianyeeT and others added 6 commits May 5, 2024 20:43
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>
Co-authored-by: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com>

@yin1999 yin1999 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一些问题(我已进行修复):

```

- `ParentClass`
- :求值为构造函数(包括类)或 `null` 的表达式。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以 - : 开头,我在另一个 PR 中已提过。

`extends` 关键字用来创建一个普通类或者内建对象的子类。

继承的 `.prototype` 必须是一个 {{jsxref("Object")}} 或者 {{jsxref("null")}}。
任何可以用 [`new`](/zh-CN/docs/Web/JavaScript/Reference/Operators/new) 调用并具有 [`prototype`](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/prototype) 属性的构造函数都可以作为父类的候选构造函数。这两个条件必须同时成立——例如,[绑定函数(bound functions)](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) 和 {{jsxref("Proxy")}} 可以被构造,但它们没有 `prototype` 属性,因此不能被子类化。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

绑定函数可不添加英文注释(因为我们已经给出了引用链接。

Comment on lines +61 to +63
| | `ChildClass` 的原型对象 | `ChildClass.prototype` 的原型对象 |
| --------------------------------- | ----------------------- | --------------------------------- |
| `extends` clause absent | `Function.prototype` | `Object.prototype` |

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不应直接忽略,我们使用格式化工具格式化 markdown 表格。

console.log(new ChildClass()); // TypeError: Derived constructors may only return object or undefined
```

如果父类构造函数返回一个对象,则在进一步初始化[类字段(class field)](/zh-CN/docs/Web/JavaScript/Reference/Classes/Public_class_fields)时,该对象将被用作派生类的 `this` 值。这种技巧被称为[“返回覆盖”](/zh-CN/docs/Web/JavaScript/Reference/Classes/Private_properties#returning_overriding_object),它允许在无关对象上定义派生类的字段(包括[私有](/zh-CN/docs/Web/JavaScript/Reference/Classes/Private_properties)字段)。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同,英文注释是不必要的;此外,请注意 URL 的片段(hash,需要找到目标文档中文版本的标题)

const m = new ReadOnlyMap([["a", 1]]); // TypeError: A read-only map must be set at construction time.
```

我们可以通过使用一个私有标志来指示是否正在构造实例来解决这个问题。然而,这种设计的一个更重要的问题是,它破坏了[里氏替换原则](https://zh.wikipedia.org/wiki/%E9%87%8C%E6%B0%8F%E6%9B%BF%E6%8D%A2%E5%8E%9F%E5%88%99),该原则规定子类应该可以替换其超类。如果函数期望使用一个 `Map` 对象,那么它也应该能够使用一个 `ReadOnlyMap` 对象,这在这里就会被打破。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

链接尽量不使用 URL 编码,以便于检查和维护。

@yin1999 yin1999 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@yin1999
yin1999 merged commit 3e787fd into mdn:main May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

l10n-zh Issues related to Chinese content.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants