简体   繁体   English

运营商的Javascript评估顺序

[英]Javascript evaluation order for operators

Which of the following expressions will always precede left to right in all browsers(particularly IE6+, F3+, Opera 9+, Chrome)? 在所有浏览器(特别是IE6 +,F3 +,Opera 9 +,Chrome)中,以下哪个表达式总是从左到右排在前面? For example the window should always alert first function then second function . 例如,窗口应始终警告first function然后second function In C they always suggest not to depend on the order of the evaluation of expressions. 在C中,他们总是建议不要依赖于表达式评估的顺序。 Is the same true for JavaScript or is Operator Precedence consistent? 对于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();

Using mozilla it appears function evaluation should be consistent in all browsers, but obviously the standard isn't always followed. 使用mozilla看起来功能评估应该在所有浏览器中都是一致的,但显然并不总是遵循标准。

Testing 测试

After some test on browsershots.org it appears all browsers follow the standard. 在browsershots.org上进行一些测试后,所有浏览器都会遵循该标准。
Generally The exception is when relying on the the valueOf method in javascript. 通常,例外是在javascript中依赖valueOf方法时。 ValueOf definitely appears to be called backwards in specific cases for google chrome. 在Google Chrome的特定情况下, ValueOf肯定会向后调用。

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

ECMAScript 5 specifies the order of evaluation of the operands for all operators. ECMAScript 5指定了所有运算符的操作数的评估顺序。 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. 编辑:另请参阅ECMAScript 3.评估顺序的定义方式相同。

Evaluating the expression into a value (eg 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: 在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"

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.". 在表达式“sqrt(9)+ sqrt(16)* sqrt(25)”中,说“乘法首先完成”是误导性的。 It is correct to say "multiplication takes precedence over addition.". 说“乘法优先于加法”是正确的。

The operations are: 这些业务是:

  1. sqrt(9) SQRT(9)
  2. sqrt(16) SQRT(16)
  3. sqrt(25) 开方(25)
  4. 4 * 5 4 * 5
  5. 3 + 20 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. 如果您有四核CPU和可以利用它的浏览器,前三个可以按任何顺序完成,或者 - gasp - 同时完成 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. 1必须在5之前完成,2和3必须在4之前完成,4必须在5之前完成。没有其他保证。

In the expression "a && (b / a)", JavaScript guarantees that a is evaluated first and b / a is not evaluated if a is zero. 在表达式“a &&(b / a)”中,JavaScript保证首先计算a,如果a为零,则不计算b / a。 Many other languages including Fortran do not guarantee this, and can get a division-by-zero exception. 包括Fortran在内的许多其他语言都不能保证这一点,并且可以获得除零除外的异常。

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)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM