상세 컨텐츠

본문 제목

[자바스크립트] 생성자 또 3가지 방법

카테고리 없음

by esoesmio 2023. 5. 4. 16:07

본문

function YalcoChicken (name, no) {
    this.name = name;
    this.no = no;
    this.introduce = function () {
        return `안녕하세요, ${this.no}호 ${this.name}점입니다!`;
    }
}

function createYalcoChicken (name, no) {
    return {
        name, no,
        introduce () {
            return `안녕하세요, ${this.no}호 ${this.name}점입니다!`;
        }
    }
}

// 객체 리터럴
const chain1 = {
    name: '판교', no: 3,
    introduce: function () {
        return `안녕하세요, ${this.no}호 ${this.name}점입니다!`;
    }
};

// 객체 반환 함수
const chain2 = createYalcoChicken('강남', 17);

// 생성자 함수
const chain3 = new YalcoChicken('제주', 24);



console.log(chain1, chain1 instanceof YalcoChicken);
console.log(chain2, chain2 instanceof YalcoChicken);
console.log(chain3, chain3 instanceof YalcoChicken);

댓글 영역