JavaScript是否具有未定义的行为? [英] Does JavaScript have undefined behaviour?

查看:65
本文介绍了JavaScript是否具有未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript是否具有未定义的行为(类似于C),或者是否完全由规范定义并具有确定性?

Does JavaScript have undefined behaviour (similar to C) or is it completely well-defined by the spec, and deterministic?

请注意,我正在丢弃实现错误和规范差异.我还丢弃了Math.random()Date.now()之类的东西.

Note that I am discarding implementation bugs and spec divergences. I am also discarding stuff like Math.random() and Date.now().

是否存在不完全由JavaScript规范确定其行为的JavaScript代码,并且因此具有未定义的行为"?

Is there a piece of JavaScript code for which the behaviour is not completely determined by the JavaScript specifications, and, as such, has "undefined behaviour"?

推荐答案

规范中有很多事情要明确地留给实现.特别是在涉及主机对象时,可能会有很多怪癖.与主机对象无关的示例:

There's a lot of things in the spec that are explicitly left to the implementation. Especially when it comes to Host Objects, there can be many quirks. Examples that have nothing to do with host objects:

15.1全局对象

全局对象的[[Prototype]]和[[Class]]内部属性的值是与实现有关的.

The values of the [[Prototype]] and [[Class]] internal properties of the global object are implementation-dependent.

15.1.2.2 parseInt(字符串,基数)

[如果有效位数太多,则mathInt可能是与实现相关的近似值,该近似值由Z以基数R表示的数学整数值.

[If there are too many significant digits] mathInt may be an implementation-dependent approximation to the mathematical integer value that is represented by Z in radix-R notation.

15.3.4.2 Function.prototype.toString

返回该函数的与实现相关的表示形式.

An implementation-dependent representation of the function is returned.

几乎所有的日期解析/字符串化算法都是与实现相关的,其中包括toLocaleStringtoStringparseDate构造函数.

Nearly all Date parse / stringifiy algorithms are implementation-dependent, this includes toLocaleString, toString, parse and the Date constructor.

15.4.4.11 Array.prototype.sort(comparefn)-可能是最好的例子:

15.4.4.11 Array.prototype.sort (comparefn) - likely the best example:

如果comparefn不是未定义的,并且不是此数组元素的一致比较函数,则sort的行为是实现定义的.

If comparefn is not undefined and is not a consistent comparison function for the elements of this array, the behaviour of sort is implementation-defined.

[…]如果proto不为null,并且存在一个整数j,使得满足以下所有条件,则sort的行为为实现定义:

[…] If proto is not null and there exists an integer j such that all of the conditions below are satisfied then the behaviour of sort is implementation-defined:

  • obj稀疏(15.4)
  • 0≤j< len

如果obj稀疏且满足以下任何条件,则还可以定义实现的行为:

The behaviour of sort is also implementation defined if obj is sparse and any of the following conditions are true:

  • obj的[[Extensible]]内部属性为false.
  • 名称是小于len的非负整数的obj的任何数组索引属性都是其[[Configurable]]属性为false的数据属性.

如果名称是小于len的非负整数的obj的任何数组索引属性是访问器属性,或者是其[[Writable]]属性的数据属性,则sort的行为也是实现定义的是错误的.

The behaviour of sort is also implementation defined if any array index property of obj whose name is a nonnegative integer less than len is an accessor property or is a data property whose [[Writable]] attribute is false.

最重要的是:

执行与实现相关的调用序列[…]

15.5.4.9 String.prototype.locale比较(that)

实现定义的方式比较两个字符串

The two Strings are compared in an implementation-defined fashion

15.5.4.11 String.prototype.replace [在替换符号中,如果数量大于组数量],则结果为实现定义.

15.5.4.11 String.prototype.replace [In replacement symbols, if the number is greater than the number of groups], the result is implementation-defined.

我将在此处停止列出,您可以在规范中进行搜索.其他值得注意的地方可能是toLocaleString方法或Math方法返回的与实现有关的近似值.

I'll just stop listing here, you can search on through the spec. Other notable places may be the toLocaleString methods, or the implementation-dependent approximations returned by the Math methods.

这篇关于JavaScript是否具有未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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