Q. How can I access the name
property of an object player
?
A. plyer[”name”] or player.name
const player = {};
player[”name”] = “haru”;
console.log(player);
// expected output: {name: "haru"}
console.log(player.name); // dot notation
// expected output: "haru"
console.log(player["name"]); //bracket notation
// expected output: "haru"
//질문
const person = {
["name is"] : "haru",
"abc ss" : 1,
}
person["age is"] = 8 //이렇게 작성하면 코드가 짧아지고 공백을 넣을 수 있다고 하심. 왜지? 그리고 공백을 넣어 사용하는 경우가 있나?
console.log(person)
//결과값
[object Object] {
abc ss: 1,
age is: 8,
name is: "haru"
}
const person = {
["name is"] : "haru",
"abc ss" : 1,
}
console.log(person["name is"])
console.log(person["abc ss"])
// "haru"
// 1
// [""], "" 키 값의 저장 형태가 다른데 동일하게 접근 가능한 이유가 궁금합니다.
// 공백을 넣어 키 값을 저장하는 예시가 있을까요?