prototype vs __proto__

김세주·2021년 1월 20일
0

JavaScript

목록 보기
8/12

function Person(){}
완전히 같다
let Person = function();

자바스크립트의 함수들은 객체기 때문에 프로퍼티를 가질 수 있다.

class Person{
    constructor(name,first,second){
        this.name = name
        this.first = first
        this.second = second
    }

    sum() {
        return this.first +this.second;
    }
}

class PersonPlus extends Person{
    constructor(name,first,second,third) {
        super(name, first, second)
        this.third = third
    }

    sum() {
        return super.sum() + this.third;
    }

    avg() {
        return (this.first +this.second+this.third) /3
    }
}
let kim = new PersonPlus('kim',10,20,30) 
console.log(kim.sum())

class 를 만들고 그 안에 생성자를 만들고 메서드를 넣는다.

profile
시간은 내 편이다.

0개의 댓글