JS 생성자 함수

HOU·2022년 4월 7일
0

JavaScript

목록 보기
6/20

생성자 함수 생성 방법

function User(name, age = 20, isAdmin) {
  // this = {}; 만들어짐
	this.name = name;
    this.isAdmin = false;
    this.age = age;
  // return this; (this가 암시적으로 변환)
    }

let user = new User('원호');

console.log(user.name); //원호
console.log(user.age); // 20

new.target

function User() {
  alert(new.target);
}

//'new'없이 호출함
User(); //undefined

//'new를 붙여 호출함
new User(); //function User {...}

Compated property key

표현식 (expression)을 이용해 객체의 key값을 정의하는 문법이다.

const name = '원호'
const age = 30

const userInfo = {
  [name]: "이름",
  [age] : "나이"
}

변수를 키값으로 사용하고 싶을 때 키에 [ + 변수명 +] 을 사용한다.

profile
하루 한 걸음 성장하는 개발자

0개의 댓글