• 杂症4-Symbol,键名转换


    题目:

    // example 1
    var a={}, b='123', c=123;  
    a[b]='b';
    a[c]='c';  
    console.log(a[b]);
    
    ---------------------
    // example 2
    var a={}, b=Symbol('123'), c=Symbol('123');  
    a[b]='b';
    a[c]='c';  
    console.log(a[b]);
    
    ---------------------
    // example 3
    var a={}, b={key:'123'}, c={key:'456'};  
    a[b]='b';
    a[c]='c';  
    console.log(a[b]);

    解析:

    这题考察的是对象的键名的转换。
    
    对象的键名只能是字符串和 Symbol 类型。
    其他类型的键名会被转换成字符串类型。
    对象转字符串默认会调用 toString 方法。
    // example 1
    var a={}, b='123', c=123;
    a[b]='b';
    
    // c 的键名会被转换成字符串'123',这里会把 b 覆盖掉。
    a[c]='c';  
    
    // 输出 c
    console.log(a[b]);
    // example 2
    var a={}, b=Symbol('123'), c=Symbol('123');  
    
    // b 是 Symbol 类型,不需要转换。
    a[b]='b';
    
    // c 是 Symbol 类型,不需要转换。任何一个 Symbol 类型的值都是不相等的,所以不会覆盖掉 b。
    a[c]='c';
    
    // 输出 b
    console.log(a[b]);
    // example 3
    var a={}, b={key:'123'}, c={key:'456'};  
    
    // b 不是字符串也不是 Symbol 类型,需要转换成字符串。
    // 对象类型会调用 toString 方法转换成字符串 [object Object]。
    a[b]='b';
    
    // c 不是字符串也不是 Symbol 类型,需要转换成字符串。
    // 对象类型会调用 toString 方法转换成字符串 [object Object]。这里会把 b 覆盖掉。
    a[c]='c';  
    
    // 输出 c
    console.log(a[b]);
  • 相关阅读:
    查找一段信号的累加峰值---verilog
    AXI_stream接口时序温习
    QAM调制---Verilog代码
    数据交织模块---Verilog代码
    卷积编码后的删余模块---Verilog代码
    数据发送模块---基于地址的检测(verilog代码)
    短训练序列---Verilog代码
    长训练序列---verilog代码
    数据扰码器---Verilog代码
    卷积编码器---Verilog代码
  • 原文地址:https://www.cnblogs.com/anbozhu7/p/11277557.html
Copyright © 2020-2023  润新知