자바스크립트 객체에 관하여

이동규·2023년 4월 30일

JAVASCRIPT

목록 보기
5/21

객체 생성 방법


let book = {// 객체 생성
    title : "자바스크립트",
    author: "이동규",
    pages:500 ,
    price:15000,
    info:function(){
       alert(this.title + "의 가격은 "+ this.price +"원입니다."); 
    }
}

function Book(title =String,autor =String,page,price){
this.title = title;
this.author = autor;
this.page = page;
this.price = price;
} // 생성자함수로 객체를 생성하는 방법

javabook = new Book("java","lee",1000,30000);// 객체 type을 reference하기 때문에 javabook의 변수명 타입을 따로 정의 할 필요가 없다.

객체를 배열에 저장하여 순회 하는 방법

const person = {
    name:"이동규",
    age :26,
    tall:168
}

const personValue = Object.values(person);// 객체의 value를 배열형태로 만드는 것이다.

for (let index = 0; index < personValue.length; index++) {
    console.log(`${personValue[index]}`);
    
}

0개의 댓글