为什么String.prototype.substr()似乎已被弃用? [英] Why String.prototype.substr() seems to be deprecated?

查看:2701
本文介绍了为什么String.prototype.substr()似乎已被弃用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ECMAScript标准中提到这里

It is mentionned on the ECMAScript standard here that :


......这些功能不被视为核心ECMAScript的一部分
语言。在编写新的ECMAScript代码时,程序员不应使用或假设存在这些
特性和行为。不鼓励ECMAScript
实现这些功能
除非实现是Web浏览器的一部分,或者需要
运行与Web浏览器相同的旧ECMAScript代码。

... These features are not considered part of the core ECMAScript language. Programmers should not use or assume the existence of these features and behaviours when writing new ECMAScript code. ECMAScript implementations are discouraged from implementing these features unless the implementation is part of a web browser or is required to run the same legacy ECMAScript code that web browsers encounter.

MDN上还有一个红色警告: String.prototype.substr()MDN doc

There is also a red warning on MDN : String.prototype.substr() MDN doc

有谁知道原因(ECMAScript)标准说,)程序员不应该使用或假设存在 String.prototype.substr

Does anyone know why (ECMAScript standard say that) programmers should not use or assume the existence of String.prototype.substr ?

推荐答案

因为它从未成为标准化语言的一部分。它根本不在ECMAScript的1或2规范中,只出现在第B.2节(附加属性)中的ECMAScript  3中(以及类似附件中的后续版本),其中说:

Because it's never been part of the standardized language. It wasn't in the ECMAScript 1 or 2 specs at all, and only appears in ECMAScript 3 in Section B.2 ("Additional Properties") (and subsequent editions in similar annexes), which said:


ECMAScript的某些实现包含了一些标准本机对象的附加属性。这个非规范性附件提出了这些属性的统一语义,而没有使这个属性或它们的语义成为这个标准的一部分。

Some implementations of ECMAScript have included additional properties for some of the standard native objects. This non-normative annex suggests uniform semantics for such properties without making the properties or their semantics part of this standard.

此外, substr 在很大程度上是多余的 substring ,但第二个参数具有不同的含义,以及 slice

Moreover, substr is largely redundant with substring, but the second argument has a different meaning, as well as with slice.

从务实的角度来说,如果你找到一个没有提供它的主流JavaScript引擎,我会感到惊讶。

In pragmatic terms, I'd be surprised if you found a mainstream JavaScript engine that didn't provide it.

这篇关于为什么String.prototype.substr()似乎已被弃用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆