FrontEnd/Javascript

Object Literal 방식

BlancPong 2022. 5. 11. 12:09
728x90

- 자바스크립트는 객체를 생성하고 프로퍼티를 지정하는 객체 리터럴(object literal) 문법을 제공한다.

 

// 내장 객체 생성
var car = new Object();
car.color = "white";
car.price = "1000만원";
car.getinfo = function() {
    document("색상: ", this.color);
    document("가격: ", this.price);
}
console.log(car);

위의 코드를 Object Literal 방식으로 바꾸면

// Object Literal 방식
var car = {
    "color": "white",
    "price": "1000만원",
    "getinfo": function() {
        document("색상: ", this.color);
        document("가격: ", this.price);
    }
}

var car = {} 는 객체를 생성하는것