运营商的Javascript评估顺序 [英] Javascript evaluation order for operators

查看:92
本文介绍了运营商的Javascript评估顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下哪个表达式总是在所有浏览器中从左到右排列(特别是IE6 +,F3 +,Opera 9 +,Chrome)?例如,窗口应始终警告第一个函数然后第二个函数。在C中,他们总是建议不要依赖于表达式评估的顺序。对于JavaScript或者运算符优先级是否一致?

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

使用 mozilla 它的功能评估应该在所有浏览器中保持一致,但显然并不总是遵循标准。

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

在browsershots.org上进行一些测试之后,所有浏览器都会遵循标准。

一般来说,
异常是在依赖在javascript中的 valueOf 方法。 ValueOf 在谷歌浏览器的特定情况下肯定会向后调用。

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;


推荐答案

ECMAScript 5 指定了所有运营商的操作数评估顺序。对于代码中的每个操作符,剪辑的评估顺序是从左到右。我不确定是否有人可以回答所有浏览器的行为。

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.

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

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

这篇关于运营商的Javascript评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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