运算符的 Javascript 评估顺序

发布于 2024-11-06 06:05:26 字数 991 浏览 4 评论 0原文

在所有浏览器(特别是 IE6+、F3+、Opera 9+、Chrome)中,以下哪个表达式始终从左到右位于前面?例如,窗口应始终提醒第一个函数,然后提醒第二个函数。在 C 中,他们总是建议不要依赖表达式求值的顺序。 JavaScript 也是如此吗?或者运算符优先级是否一致?

function first(){
    alert('first function');
    return 0;
}
function second(){
    alert('second function');
    return 23;
}
first() + second();
first() - second();
first() * second();
first() / second();
first() < second();
first() > second();

使用 mozilla 看来函数评估在所有浏览器中应该是一致的,但显然该标准并不总是得到遵守。

测试

在 browsershots.org 上进行一些测试后,似乎所有浏览器都遵循标准。
一般来说 例外情况是依赖 javascript 中的 valueOf 方法。在 google chrome 的特定情况下,ValueOf 肯定会被向后调用。

// The following alerts second then first in google chrome
first.valueOf = function(){alert('first');};
second.valueOf = function(){alert('second');};
first > second;

Which of the following expressions will always precede left to right in all browsers(particularly IE6+, F3+, Opera 9+, Chrome)? For example the window should always alert first function then second function. In C they always suggest not to depend on the order of the evaluation of expressions. Is the same true for JavaScript or is Operator Precedence consistent?

function first(){
    alert('first function');
    return 0;
}
function second(){
    alert('second function');
    return 23;
}
first() + second();
first() - second();
first() * second();
first() / second();
first() < second();
first() > second();

Using mozilla it appears function evaluation should be consistent in all browsers, but obviously the standard isn't always followed.

Testing

After some test on browsershots.org it appears all browsers follow the standard.
Generally
The exception is when relying on the the valueOf method in javascript. ValueOf definitely appears to be called backwards in specific cases for google chrome.

// The following alerts second then first in google chrome
first.valueOf = function(){alert('first');};
second.valueOf = function(){alert('second');};
first > second;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

会发光的星星闪亮亮i 2024-11-13 06:05:26

ECMAScript 5 指定所有运算符的操作数求值顺序。对于代码片段中的每个运算符,评估顺序都是从左到右。不过,我不确定是否有人可以回答所有浏览器的行为。

编辑:另请参阅 ECMAScript 3 . 评估顺序的定义方式相同。

ECMAScript 5 specifies the order of evaluation of the operands for all operators. In the case of every operator in your code snip the evaluation order is left-to-right. I'm not sure anyone could answer about the behavior of all browsers though.

Edit: See also ECMAScript 3. Evaluation order is defined the same way.

衣神在巴黎 2024-11-13 06:05:26

将表达式求值为值(例如涉及函数调用)总是从左到右进行。

但是,一旦比较两个值,它们就不会转换为基元,以便以从左到右的方式进行实际比较。例如,在 Chrome 中尝试以下操作:

var l = {valueOf: function() { alert("L"); }};
var r = {valueOf: function() { alert("R"); }};

l < r; //alerts "L", then "R"
l > r; //alerts "R", then "L"

Evaluating the expression into a value (e.g. involving a function call) is always done left to right.

However, once you are comparing two values, they are not converted into primitives in order to do the actual comparison in a left to right fashion. Try the following in Chrome, for example:

var l = {valueOf: function() { alert("L"); }};
var r = {valueOf: function() { alert("R"); }};

l < r; //alerts "L", then "R"
l > r; //alerts "R", then "L"
青衫负雪 2024-11-13 06:05:26

运算符优先级和求值顺序是两个完全不同的东西。在表达式“sqrt(9) + sqrt(16) * sqrt(25)”中,“首先进行乘法”是一种误导。 “乘法优先于加法”这句话是正确的。

这些操作是:

  1. sqrt(9)
  2. sqrt(16)
  3. sqrt(25)
  4. 4 * 5
  5. 3 + 20

前三个操作可以按任何顺序完成,或者 -- 喘息 -- 同时(如果您有)四核 CPU 和可以利用它的浏览器。 1 必须在 5 之前完成,2 和 3 必须在 4 之前完成,4 必须在 5 之前完成。没有其他保证。

在表达式“a && (b / a)”中,JavaScript 保证首先计算 a,如果 a 为零,则不计算 b / a。包括 Fortran 在内的许多其他语言不保证这一点,并且可能会出现被零除异常。

Operator precedence and order of evaluation are two entirely different things. In the expression "sqrt(9) + sqrt(16) * sqrt(25)" it is misleading to say that "the multiplication is done first.". It is correct to say "multiplication takes precedence over addition.".

The operations are:

  1. sqrt(9)
  2. sqrt(16)
  3. sqrt(25)
  4. 4 * 5
  5. 3 + 20

The first three could be done in any order, or -- gasp -- simultaneously if you have a four-core CPU and a browser that can take advantage of it. 1 must be done before 5, 2 and 3 must be done before 4, and 4 must be done before 5. There are no other guarantees.

In the expression "a && (b / a)", JavaScript guarantees that a is evaluated first and b / a is not evaluated if a is zero. Many other languages including Fortran do not guarantee this, and can get a division-by-zero exception.

つ可否回来 2024-11-13 06:05:26

我不确定您的用例是什么,但这可能是一个选择:

function add() {
  var retval = 0;
  for (var i = 0; i < arguments.length; i++) {
    retval += arguments[i];
  }
  return retval;
}

function echoNum(num) {
  alert("Num: " + num);
  return num;
}

alert("Result: " + add(echoNum(1), echoNum(2)));

I'm not sure what your use-case is, but this might be an option:

function add() {
  var retval = 0;
  for (var i = 0; i < arguments.length; i++) {
    retval += arguments[i];
  }
  return retval;
}

function echoNum(num) {
  alert("Num: " + num);
  return num;
}

alert("Result: " + add(echoNum(1), echoNum(2)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文