What is the order of evaluation for function arguments in Javascript?

前端 未结 3 910
无人及你
无人及你 2020-12-01 16:05

According to my tests it is always left-to-right

>> console.log( console.log(1), console.log(2) );
1
2
undefined undefined

but I can\

3条回答
  •  有刺的猬
    2020-12-01 16:57

    It's defined here:

    The production ArgumentList : ArgumentList , AssignmentExpression is evaluated as follows:

    1. Let precedingArgs be the result of evaluating ArgumentList.
    2. Let ref be the result of evaluating AssignmentExpression.
    3. Let arg be GetValue(ref).
    4. Return a List whose length is one greater than the length of precedingArgs and whose items are the items of precedingArgs, in order, followed at the end by arg which is the last item of the new list.

    Read here: http://es5.github.com/#x11.2.4

    When a function is invoked, the passed-in arguments are evaluated from left to right.

提交回复
热议问题