속성 |
값 |
이름 |
삼겹살 |
무게 |
100g |
가격 |
1690원 |
메서드 |
설명 |
calculator(<무게>) |
무게를 기반으로 가격을 계산 |
생성자 함수 이름을 Product로 가지고 product.calculate(200)을 입력할 때 3380을 출력하는 생성자 함수를 만들어라
더보기
생성자 함수 |
function Product(name, weight, price){ this.name=name; this.weight=weight; this.price=price;
this.calculate=function(weight){ return this.price*(weight/this.weight); } } let product=new Product('삼겹살', 100, 1690); alert('가격은 '+product.calculate(Number(prompt('무게 입력')))+'원 입니다.');
|
프로토타입 |
function Product(name, weight, price){ this.name=name; this.weight=weight; this.price=price; }
Product.prototype.calculate=function(weight){ return this.price*(weight/this.weight); }
let product=new Product('삼겹살', 100, 1690); alert('가격은 '+product.calculate(Number(prompt('무게 입력')))+'원 입니다.');
|
클래스 |
class Product{ constructor(name, weight, price){ this.name=name; this.weight=weight; this.price=price; } calculate(weight){ return this.price*(weight/this.weight); } } const product=new Product('삼겹살', 100, 1690); alert('가격은 '+product.calculate(Number(prompt('무게 입력')))+'원 입니다.');
|