JavaScript第七种数据类型,Symbol(二)获取与使用

Symbol的获取和使用

  const student = {
    name: "小明",
    age: 12,
    [Symbol("level")]: "优秀",
    [Symbol("level")]: "有钱"
  }

获取key值

  console.log(Object.keys(student))  // ["name","age"]

  let pros = Object.getOwnPropertyNames(student)
  console.log(pros)  // ["name","age"]

  for(let pro in student){
    console.log(pro)  // name age
  }

获取symbol

  let symList = Object.getOwnPropertySymbols(student)
  console.log(symList)  // [Symbol(level),Symbol(level)

遍历

  for(let pro of symList){
    console.log(student[pro])  // 优秀  有钱
  }
posted @ 2021-07-29 17:42  `Duet`  阅读(300)  评论(0编辑  收藏  举报