Skip to content

Commit e2c5b8a

Browse files
author
yibuyisheng
committed
7.4.3
1 parent 5fcca04 commit e2c5b8a

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

md/7/7.4.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,15 @@ for (const x of obj) {
7474
// hello
7575
// world
7676
```
77+
78+
### 7.4.3 JavaScript 标准库命名冲突举例
79+
80+
或许你觉得命名冲突不要紧,下面有三个例子,展示了在 JavaScript 标准库演化过程中命名冲突带来的问题:
81+
82+
- 在引入新方法 `Array.prototype.values()` 的时候,破坏了已有的 `with` 与数组结合使用的代码,它会在外面的作用域中隐藏一个 `values` 变量([bug report 1](https://bugzilla.mozilla.org/show_bug.cgi?id=881782)[bug report 2](https://bugzilla.mozilla.org/show_bug.cgi?id=883914))。因此,引入了一种隐藏属性的机制( System.unscopables )。
83+
- `String.prototype.contains` 和 MooTools 中添加的一个方法冲突了,不得不重命名成 `String.prototype.includes`[bug report](https://bugzilla.mozilla.org/show_bug.cgi?id=789036) )。
84+
- ES2016 中的 `Array.prototype.contains` 方法也和 MooTools 中添加的方法冲突了,不得不重命名成 `Array.prototype.includes`[bug report](https://bugzilla.mozilla.org/show_bug.cgi?id=1075059) )。
85+
86+
相比之下,通过属性键 `System.iterator` 给一个对象增加可迭代性就不会引起问题,因为该键并不会和任何键冲突。
87+
88+
上述例子展示了成为一门 web 开发语言意味着什么:向后兼容是至关重要的,这就是为什么在改进语言的时候,偶尔妥协是必须的。这种向后兼容的好处就是,改进老的代码库很轻松,因为新的 ECMAScript 版本绝不会(好吧,是几乎不会)破坏这些老代码。

0 commit comments

Comments
 (0)