JS의 인스턴스란?

ansunny1170·2022년 6월 14일
0

단순참조

목록 보기
14/23

instance

붕어빵을 만들 때, 붕어빵 틀에 반죽을 부어 구어낸다.
붕어빵: 객체
붕어빵 틀: 클래스

비유에 따르면 클래스객체를 만든다. 라고 할 수 있다.

하지만 앞으로 클래스인스턴스를 만든다. 라는 말을 많이 들을 것이다.

객체 = 인스턴스로 볼 수 있겠다.(약간의 차이는 존재한다!)

예제

/* function 키워드 사용*/
function User(name, age) {
  this.name = name;
  this.age = age;
  function.prototype.is = function() {
    console.log(`The person name is ${this.name}, age is ${this.age}!`);
  };
}
/* calss 키워드 사용*/
class User1 {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  is() {
    console.log(`The person name is ${this.name}, age is ${this.age}!!`);
  }
}

const korean = new User('마쟈용', 3);
const korean1 = new User1('마쟈용', 3);

console.log(korean); //User {name: '마쟈용', age: 3, is: f}
console.log(korean1);

korean.is(); //The person name is 마쟈용, age is 3!
korean1.is();

JS에는 클래스를 선언하는 두가지 방법이 있다. function, class 키워드를 사용한 방법이다.

profile
공정 설비 개발/연구원에서 웹 서비스 개발자로 경력 이전하였습니다. Node.js 백엔드 기반 풀스택 개발자를 목표로 하고 있습니다.

0개의 댓글