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