javascript - js对象 属性的访问和创建
问题描述
一个有意思的问题:
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
这里其实涉及到的就是[]运算符 和.运算符 相关知识。
附上相关规则和网址,你们自己研究吧:
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. angular.js - 有没有不需要先git clone xxxx的angular2的教程?2. java - 当在子类中声明一个父类中存在的变量后,自动调用的父类构造函数不起作用。3. 用Java写发送邮件的程序,经常被当做垃圾邮件处理怎么解决4. angular.js - angularjs 如何用一组按钮完成单选5. angular.js - angular双向绑定机制异常6. java - Spring boot启动时报错?7. angular.js - angularJs里面的这种判断自动生成的注释可以去掉吗?8. android - recyclerview显示错乱9. angular.js - 报这个错是什么原因呢?没有显示,因为报错,可是controller里没有这个错10. angular.js - 求Angular ui-router 多层嵌套的Demo!