javascript - js對(duì)象 屬性的訪問和創(chuàng)建
問題描述
一個(gè)有意思的問題:
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
這里其實(shí)涉及到的就是[]運(yùn)算符 和.運(yùn)算符 相關(guān)知識(shí)。
附上相關(guān)規(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:其實(shí)就是個(gè) Object toString 的問題。
相關(guān)文章:
1. linux - Ubuntu下編譯Vim8(+python)無數(shù)次編譯失敗2. java - 在搭建ssm的過程中 用junit測(cè)試Dao層 怎么樣都報(bào)錯(cuò) 說連接不上jdbc3. javascript - 如何判斷用戶切換到了當(dāng)前標(biāo)簽頁?4. java - Mybatis關(guān)聯(lián)查詢5. css - 移動(dòng)端 盒子內(nèi)加overflow-y:scroll后 字體會(huì)變大6. java - 新手做一個(gè)安卓視頻播放器,想實(shí)現(xiàn)一個(gè)進(jìn)度條,按鈕那種在視頻下方懸浮的功能,不知道思路!7. javascript - webpack 報(bào)錯(cuò) 新人 求解8. javascript - H5頁面怎么查看console信息?9. nginx 80端口反向代理多個(gè)域名,怎樣隱藏端口的?10. 前端 - CSS3 box-shadow如何設(shè)置,或者用什么方法可以產(chǎn)生圖中這樣陰影的效果。
