javascript - js物件 屬性的存取和創建
ringa_lee
ringa_lee 2017-07-05 10:58:49
0
1
957

一個有意思的問題:

  1. var a = new Object();  
    var b = new Object();  
    var c = new Object();
      
    c[a] = a;  
    c[b] = b;
      
    console.log(c[a] === a); //输出什么?  ---> false
    console.log(c[b] === b); //输出什么?  ---> true
  2. var a = new Object();  
    var b = new Object();  
    var c = new Object();
      
    c.a=a;  
    c.b=b;
      
    console.log(c.a === a); //输出什么?  ---> true
    console.log(c.b === b); //输出什么?  ---> true

這裡其實牽涉到的就是[]運算子 和.運算子 相關知識。

附上相關規則和網址,你們自己研究吧:

MemberExpression : MemberExpression [ Expression ]

  1. Let baseReference be the result of evaluating MemberExpression.

  2. Let baseValue be GetValue(baseReference).

  3. ReturnIfAbrupt(baseValue).

  4. #Let propertyNameReference be the result of evaluating Expression.

  5. Let propertyNameValue be GetValue(propertyNameReference).

  6. #ReturnIfAbrupt(propertyNameValue).

  7. Let bv be RequireObjectCoercible(baseValue).

  8. #ReturnIfAbrupt(bv).

  9. #Let propertyKey be ToPropertyKey(propertyNameValue).

  10. #ReturnIfAbrupt(propertyKey).

  11. 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.

  12. 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

  1. #Let baseReference be the result of evaluating MemberExpression.

  2. Let baseValue be GetValue(baseReference).

  3. ReturnIfAbrupt(baseValue).

  4. #Let bv be RequireObjectCoercible(baseValue).

  5. #ReturnIfAbrupt(bv).

  6. #Let propertyNameString be StringValue of IdentifierName

  7. #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.

  8. 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#####
ringa_lee
ringa_lee

ringa_lee

全部回覆(1)
某草草

其實就是個 Object toString 的問題。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板