[英]How can I access properties inside a symbol type object in JavaScript?
const classRomm = {
[Symbol('Mia')]: { grade: 50, gender: 'female' },
[Symbol('Gilbert')]: {grade: 80, gender: 'male'},
};
let persons = Object.getOwnPropertySymbols(classRomm);
for(let i of persons) {
console.log(i);
}
In above, the for of loop
returns Symbol(Mia)
and Symbol(Gilbert)
.在上面, for of loop
返回Symbol(Mia)
和Symbol(Gilbert)
。 But these are not accessible.但这些都无法访问。 I want to reach grade and gender property but I can not do that.我想达到年级和性别属性,但我做不到。 How can I do it?我该怎么做?
You need to take the collected symbols as accessor for the object.您需要将收集到的符号作为 object 的访问器。
const classRoom = { [Symbol('Mia')]: { grade: 50, gender: 'female' }, [Symbol('Gilbert')]: {grade: 80, gender: 'male'}, }; let persons = Object.getOwnPropertySymbols(classRoom); for (const person of persons) console.log(classRoom[person]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.