Javascript的引用顺序 [英] Javascript order of referencing

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

问题描述

以下语句的运行顺序是什么?运行时是从右到左执行它吗?

What order does the following statement run? Does the runtime execute it from right to left?

  length = i = test = output = null;


推荐答案

是的,因为分配表达式不是左手边的表达本身,嵌套是

Yes, since an assignment expression is not a lefthand-side expression itself, the nesting is

(length = (i = (test = (output = null))));

从外向内执行,从左到右解决引用(首先获得 length 变量,然后评估右侧,即获取 i 变量并评估...)。这意味着输出是第一个被分配的变量,然后是 test ,然后是 i ,最后长度:从右到左,如果你愿意的话。由于赋值运算符总是产生其右操作数,因此所有内容都将被赋予相同的(最里面的,最右边的)值: null

It is executed from outside-in, solving references left-to-right (first getting the length variable, then evaluating the right side, which is getting the i variable and evaluating …). That means output is the first variable that is getting assigned to, then test, then i, and last length: "right-to-left" if you want. Since the assignment operator always yields its right operand, everything will be assigned the same (innermost, rightmost) value: null.

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

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