javascript - js對象 屬性的訪問和創(chuàng)建
問題描述
一個有意思的問題:
var a = new Object(); var b = new Object(); var c = new Object(); c[a] = a; c[b] = b; console.log(c[a] === a); //輸出什么? ---> falseconsole.log(c[b] === b); //輸出什么? ---> true
var a = new Object(); var b = new Object(); var c = new Object(); c.a=a; c.b=b; console.log(c.a === a); //輸出什么? ---> trueconsole.log(c.b === b); //輸出什么? ---> true
這里其實涉及到的就是[]運算符 和.運算符 相關知識。
附上相關規(guī)則和網(wǎng)址,你們自己研究吧:
MemberExpression : MemberExpression [ Expression ]
Let baseReference be the result of evaluating MemberExpression.
Let baseValue be GetValue(baseReference).
ReturnIfAbrupt(baseValue).
Let propertyNameReference be the result of evaluating Expression.
Let propertyNameValue be GetValue(propertyNameReference).
ReturnIfAbrupt(propertyNameValue).
Let bv be RequireObjectCoercible(baseValue).
ReturnIfAbrupt(bv).
Let propertyKey be ToPropertyKey(propertyNameValue).
ReturnIfAbrupt(propertyKey).
If the code matched by the syntactic production that is being evaluated is strict mode code, let strict be true, else let strict be false.
Return a value of type Reference whose base value is bv and whose referenced name is propertyKey, and whose strict reference flag is strict.
MemberExpression : MemberExpression . IdentifierName
Let baseReference be the result of evaluating MemberExpression.
Let baseValue be GetValue(baseReference).
ReturnIfAbrupt(baseValue).
Let bv be RequireObjectCoercible(baseValue).
ReturnIfAbrupt(bv).
Let propertyNameString be StringValue of IdentifierName
If the code matched by the syntactic production that is being evaluated is strict mode code, let strict be true, else let strict be false.
Return a value of type Reference whose base value is bv and whose referenced name is propertyNameString, and whose strict reference flag is strict.
CallExpression : CallExpression [ Expression ]
Is evaluated in exactly the same manner as MemberExpression : MemberExpression [ Expression ] except that the contained CallExpression is evaluated in step 1.
CallExpression : CallExpression . IdentifierName
Is evaluated in exactly the same manner as MemberExpression : MemberExpression . IdentifierName except that the contained CallExpression is evaluated in step 1.
ECMAScript 2015 #sec-property-accessors
問題解答
回答1:其實就是個 Object toString 的問題。
相關文章:
1. mysql - 數(shù)據(jù)庫建表方面的問題?2. javascript - angularJS指令如何暴露API給外面的controller使用?3. mysql - 我用SQL語句 更新 行的時候,發(fā)現(xiàn)全部 中文都被清空了,請問怎么解決?4. python - xpath提取網(wǎng)頁路徑?jīng)]問題,但是缺失內容?5. javascript - IOS微信audio標簽不能通過touchend播放6. javascript - 求教各位,本地HTML頁面怎么在DIV中嵌套服務器上的頁面內容?不用iframe。7. python-mysql Commands out of sync8. java中這個頁面默認是utf-8編碼的,1輸出亂碼可以理解,可是2就不理解了?9. [前端求職必看]前端開發(fā)面試題與答案精選_大綱10. docker不顯示端口映射呢?
