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
原文地址:https://www.cnblogs.com/Ananiah/p/11071234.html
时间: 2024-10-20 14:21:23