這是因爲C.method
返回類似
{ base: C, referencedName: "method", strict: strictFlag }
基準時call it,JS獲得使用GetValue與參考值的功能,並提供參考(C
)作爲this
值的基礎。
CallExpression : MemberExpressionArguments
1. Let ref be the result of evaluating MemberExpression. // <-- The reference
2. Let func be ? GetValue (ref). // <-- The function
4. If Type (ref) is Reference , then
a. If IsPropertyReference(ref) is true, then
i. Let thisValue be GetThisValue (ref). // <-- C
然而,當你使用comma operator,你直接獲得的功能,而不是參考。
Expression : Expression,AssignmentExpression
1. Let lref be the result of evaluating Expression.
2. Perform ? GetValue (lref). // <-- 0
3. Let rref be the result of evaluating AssignmentExpression.
4. Return ? GetValue (rref). // <-- The function
由於沒有參考,JS無法知道基本對象,所以當你把它提供undefined
作爲this
值。
CallExpression : MemberExpressionArguments
1. Let ref be the result of evaluating MemberExpression. // <-- The function
2. Let func be ? GetValue (ref). // <-- The function
5. Else Type (ref) is not Reference ,
1. Let thisValue be undefined. // <-- undefined
上下文從類更改爲全局,所以'this'未定義。 – Li357
您可能想要閱讀[this'如何使用對象方法的MDN描述](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Thisis_As_an_object_method)。 – 4castle
@ 4castle如果一個內存是通過'var'聲明和初始化的,這是有道理的;但是'(0,C.method)'表達式如何分配新的內存插槽? – steveyang