Javascript order of referencing

前端 未结 4 1241
不思量自难忘°
不思量自难忘° 2020-12-07 05:25

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

  length = i = test = output = null;
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 06:17

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

    (length = (i = (test = (output = 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.

提交回复
热议问题