在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML/ 為什么console.log(obj.prop = value),輸出 valu

為什么console.log(obj.prop = value),輸出 value

const obj = {};
console.log(obj.name = 'hello');// 為什么這個(gè)地方輸出 hello

備注,其實(shí)很少會(huì)這樣使用,只是在看ts中定義枚舉的時(shí)候看到了類似的代碼,所以理解所以提出來(lái)看看

回答
編輯回答
柒槿年

賦值表達(dá)式的返回值就是右值。(也存在 this 丟失問(wèn)題。

2017年3月15日 19:57
編輯回答
熟稔

那是瀏覽器輸出的

clipboard.png

2018年4月10日 23:18
編輯回答
蔚藍(lán)色

首先,參考一下ECMA對(duì)賦值運(yùn)算的定義:

The production AssignmentExpression : LeftHandSideExpression = AssignmentExpression is evaluated as follows:

1.Let lref be the result of evaluating LeftHandSideExpression.
2.Let rref be the result of evaluating AssignmentExpression.
3.Let rval be GetValue(rref).
4.Throw a SyntaxError exception if the following conditions are all true:

  • Type(lref) is Reference is true
  • IsStrictReference(lref) is true
  • Type(GetBase(lref)) is Environment Record
  • GetReferencedName(lref) is either "eval" or "arguments"

5.Call PutValue(lref, rval).
6.Return rval.

所以說(shuō),obj.name = 'hello'這個(gè)表達(dá)示最后return 'hello'.
從內(nèi)存角度分析。執(zhí)行這個(gè)表達(dá)式的時(shí)候,開(kāi)辟一塊內(nèi)存,放置hello,obj這個(gè)對(duì)象的一個(gè)屬性name指向這塊內(nèi)存。執(zhí)行完這個(gè)表達(dá)式后,返回了hello這個(gè)內(nèi)存的地址。
最后console.log()的時(shí)候就打印出了指向內(nèi)存地址的值。

2017年2月19日 18:50