Skip to content
On this page

프로토타입(prototype)

Title
프로토타입(prototype)
Category
JavaScript
Tags
Aliases
프로토타입(prototype)프로토타입Prototype
Related
Created
2 years ago
Updated
last year

TL;DR

animal-rabbit-extends

  • 현실에 존재하는 것 중 가장 좋은 본보기를 원형(prototype)으로 선택한다.
  • 컨텍스트(Context)에 따라 '범주', 즉 '의미'가 달라진다.
js
function Foo(y) {
	this.y = y;
}

Foo.prototype.x = 10;
Foo.prototype.calculate = function (z) {
	return this.x + this.y + z;
};

const b = new Foo(20);
const c = new Foo(30);

b.calculate(30); // 60
c.calculate(30); // 70

console.log(
	b.__proto__ === Foo.prototype, // true
	c.__proto__ === Foo.prototype, // true

	b.constructor === Foo, // true
	c.constructor === Foo, // true
	Foo.prototype.constructor === Foo, // true

	b.calculate === b.__proto__.calculate, // true
	b.calculate === Foo.prototype.calculate, // true
);
function Foo(y) {
	this.y = y;
}

Foo.prototype.x = 10;
Foo.prototype.calculate = function (z) {
	return this.x + this.y + z;
};

const b = new Foo(20);
const c = new Foo(30);

b.calculate(30); // 60
c.calculate(30); // 70

console.log(
	b.__proto__ === Foo.prototype, // true
	c.__proto__ === Foo.prototype, // true

	b.constructor === Foo, // true
	c.constructor === Foo, // true
	Foo.prototype.constructor === Foo, // true

	b.calculate === b.__proto__.calculate, // true
	b.calculate === Foo.prototype.calculate, // true
);

proto

js
let human = {
	teeth: 32,
};

let gwen = {
	// We added this line:
	__proto__: human,
	age: 19,
};
let human = {
	teeth: 32,
};

let gwen = {
	// We added this line:
	__proto__: human,
	age: 19,
};

root

상속

References

Released under the MIT License.