javascript

Javascript 객체 리터럴 (Literals)

wefree 2023. 12. 14. 23:07
const person = {
    firstName: "Doe",
    lastName: "John",
    print() {
        console.log(`Name: ${this.firstName} ${this.lastName}`)
    },
}

person.firstName
person["firstName"]
person["first" + "Name"]

person.lastName = "Smith"
person.print(); // Doe Smith

// key 는 항상 문자열로 변환되어 저장된다.
// 문자가 아닌 key 로 조회하면, key 값을 문자열로 변환 후 조회한다 !!!!!
const years = {1999: 'Good', 2020: 'Bad', true: 'SoSo'}
years["1999"]  // GOOD
years[1999] // GOOD
years["true"] // SoSo
years[true] // SoSo