Javascript Class

정종찬·2022년 4월 21일

/*
클래스 (class)

함수 생성자

*/

let brick = {
    left:0, right:0, top:0, bottom:0,
    column:0, row:0, 
}
벽돌 left, top, right, bottom, col, row, pos + 움직이는 기능

function Brick(left, top, right, bottom) { // 함수생성자 새로운 데이터 타입 네이밍은 대문자로 시작
    this.left = left,
    this.top = top,
    this.right = right,
    this.bottom = bottom
    /* , movingAction = function() { this.left++; console.log('내가 움직이고 있어') } */
}

Brick.prototype.movingAction = function() { this.left++; console.log('내가 움직이고 있어') }
// for문 안에서 생성자를 통해 함수실행시 함수식이 포문숫자만큼 실행된다. 
// 프로토타입으로 정해주면 그걸 참고해여 포문에서 한번만 사용 객체마다 같은 함수를 실행시키지않고 프로토타입 하나로 사용 


for(let i = 0; i < 20; i++) 
{
    let tempBrick = new Brick(0, 0, 10, 10);
    tempBrick.movingAction();
} 




//////////////////////////////////////////////////////////////////////////

class Brick {
    constructor(left, top, right, bottom) {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    }

    movingAction() { // 자동으로 프로토타입 적용
        this.left++; console.log('내가 움직이고 있어');
    }
}

클래스로 객체의 설계도를 만든다.
명사로 지칭되는 객체를 설계한다. 자동차(속성과 기능), 책(속성과 기능), 몬스터(속성과 기능), 사람...

profile
dalssenger

0개의 댓글