프로토타입 객체들간의 공유하는 속성을 넣어두는 것 !
-> 중복을 줄여 한번에 수정하고 한번에 삭제할 수 있게 하기위해 사용한다.
var prototype = {
type: 'card',
attack: function() {},
defend: function(){}
};
function cardFactory(name, att, hp) {
var card = Object.create(prototype)
card.name = name;
card.att = att;
card.hp = hp;
return card;
}
var card1 = 카드공장('card1', 10, 20);
var card2 = 카드공장('card2', 50, 100);
var prototype = {
type: 'card'
}
function Card(name, att, hp) {
this.name = name;
this.att = att;
this.hp = hp;
}
Card.prototype = prototype;
var card1 = new Card('card1', 5, 10);
-> 실수로 new
를 붙이지 않으면 Card안의 this
는 window
가 되기 때문에 window.name
이 'card1'이 된다.
function Card(name, att, hp) {
"use strict"
this.name = name;
this.att = att;
this.hp = hp;
}
엄격모드 사용하는 경우
new
를 쓰지 않았을때 에러가 남. 엄격모드 아래의 코드들이 적용됨.