Prototype이란?

// 1. 프로토타입을 이용하지 않을 때
function KoreanFood1(name) {
	this.country = 'Korea';
	this.foodName = name;
	
	this.printFood = function () {
		console.log(this.country);
		console.log(this.foodName);
	};
}

const kf1 = new KoreanFood1('치킨');
const kf2 = new KoreanFood1('불고기');

Untitled

// 2. 프로토타입 이용 할 경우
function KoreanFood2(name) {
	this.foodName = name;
}

KoreanFood2.prototype.country = 'Korea';
KoreanFood2.prototype.printFood = function () {
	console.log(this.country);
	console.log(this.foodName);
};

const kf3 = new KoreanFood2('치킨');
const kf4 = new KoreanFood2('불고기');

Untitled

프로토타입 체인(Prototype Chain)

Untitled