es6之Symbol

Symbol可以单独定义,值不会相等

{
    //声明
    let a1=Symbol();
    let a2=Symbol();
    console.log("Symbol",a1===a2);  //false false
    let a3=Symbol.for("a3");
    let a4=Symbol.for("a3");
    console.log(a3===a4) //true

}
{
    let a1=Symbol.for("abc");
    let obj={
        [a1]:"123",
        "abc":345,
        "c":456
    }
    console.log(obj); 

    //for of 取不到key value值
    for(let [key,value] of Object.entries(obj)){
        console.log("let of",key,value);

    }
    //获取Symbol定义的key值
    Object.getOwnPropertySymbols(obj).forEach(function(item){
        console.log("symbolKey",obj[item]);  //123
    })
    //获取所有的,包括Symbol定义的key value值
    Reflect.ownKeys(obj).forEach(function(item){
        console.log("ownkeys",item,obj[item]); //abc 345,c 456,Symbol(abc) 123
    })

  

posted @ 2017-11-20 13:50  karila  阅读(118)  评论(0编辑  收藏  举报