ES6-Symbol的用法 ,symbol在对象中的应用,改变值

ES6-Symbol的用法,,symbol在对象中的应用,改变值

let a = new String;
let b = new Number;
let c = new Boolean;
let d = new Array;
let e = new Object;


//Symbol
//声明Symbol 
//Symbol类型是一个全局的原始类型
let f = Symbol();
console.log(typeof(f)) //symbol

let g = Symbol('ananiah');
console.log(g)  //Symbol(ananiah)
console.log(g.toString())  //Symbol(ananiah) 转为字符串

//symbol在对象中的应用
let ananiah = Symbol();
let obj = {
    [ananiah] : '大诶啊'
}
console.log(obj[ananiah]) //大诶啊

//改变值
obj[ananiah] = 'web';
console.log(obj[ananiah]) //web

// 用法
let noage = {name:'ananiah',skill:'eat'};
let age  = Symbol();
noage[age] = 18;
console.log(noage) // {name: "ananiah", skill: "eat", Symbol(): 18}
for(let item in noage){
    console.log(noage[item])  //ananiah eat 
}
console.log(noage[age]) // 18

 

posted @ 2019-06-23 00:56  Ananiah  阅读(942)  评论(0编辑  收藏  举报