javascript (new)

CHAN YE·2022년 10월 14일
0

new를 붙이면 객체생성

function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

console.log(new Food('cheese', 5).name);
// expected output: "cheese"

붙이지 않으면 함수호출

function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
  return name;
}

console.log(Food('cheese', 5));
// expected output: "cheese"

new를 사용하지 않고 call 사용

function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}
var food = {}
Food.call(food,'cheese', 5);
console.log(food.name);
// expected output: "cheese"
profile
php웹개발자

0개의 댓글