| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
JavaScript , (Java C++ ) .
, JavaScript . . , null . null , . JavaScript (static dispatching) .
JavaScript , . , classes .
JavaScript , . , .
JavaScript "" ( ) . , , .
:
ECMAScript someObject.[[Prototype]] someObject . [[Prototype]] Object.getPrototypeOf() Object.setPrototypeOf() . JavaScript __proto__ . obj.__proto__ , obj.[[Prototype]] . Object.getPrototypeOf(obj) .
[[Prototype]] , . , __proto__ . { __proto__: ... } , obj.__proto__ .
{ a: 1, b: 2, __proto__: c } , c(null ) [[Prototype]] . , a b . [[Prototype]] " " , .
.
const o = {
a: 1,
b: 2,
// __proto__ [[Prototype]] .
// .
__proto__: {
b: 3,
c: 4,
},
};
// o.[[Prototype]] 'b' 'c' .
// o.[[Prototype]].[[Prototype]] Object.prototype ( ).
// , o.[[Prototype]].[[Prototype]].[[Prototype]] null.
// null [[Prototype]] .
// .
// {a: 1, b: 2} ---> {b: 3, c: 4} ---> Object.prototype ---> null
console.log(o.a); // 1
// o 'a' ? , 1.
console.log(o.b); // 2
// o 'b' ? , 2.
// 'b' . " (property shadowing)" .
console.log(o.c); // 4
// o 'c' ? , .
// o.[[Prototype]] 'c' ? , 4.
console.log(o.d); // undefined
// o 'd' ? , .
// Is there a 'd' own property on o.[[Prototype]]? No, check its prototype.
// o.[[Prototype]] 'd' ? , .
// o.[[Prototype]].[[Prototype]] Object.prototype,
// 'd' . .
// o.[[Prototype]].[[Prototype]].[[Prototype]] null, ,
// , undefined .
" " . , getter or setter .
, , .
const o = {
a: 1,
b: 2,
// __proto__ [[Prototype]] .
// .
__proto__: {
b: 3,
c: 4,
__proto__: {
d: 5,
},
},
};
// { a: 1, b: 2 } ---> { b: 3, c: 4 } ---> { d: 5 } ---> Object.prototype ---> null
console.log(o.d); // 5
JavaScript "" . JavaScript . " " (, " " " , method overriding" ).
, this .
const parent = {
value: 2,
method() {
return this.value + 1;
},
};
console.log(parent.method()); // 3
// parent.method , 'this' .
// .
const child = {
__proto__: parent,
};
console.log(child.method()); // 3
// child.method , 'this' .
// ,
// 'value' . 'value' ,
// [[Prototype]] , parent.value.
child.value = 4; // 'value' 4 .
// 'value' .
// .
// { value: 4, __proto__: { value: 2, method: [Function] } }
console.log(child.method()); // 5
// Since child now has the 'value' property, 'this.value' means
// 'value' 'this.value' child.value .
, . . , getValue '' . .
const boxes = [
{ value: 1, getValue() { return this.value; } },
{ value: 2, getValue() { return this.value; } },
{ value: 3, getValue() { return this.value; } },
];
, . , getValue [[Prototype]] .
const boxPrototype = {
getValue() {
return this.value;
},
};
const boxes = [
{ value: 1, __proto__: boxPrototype },
{ value: 2, __proto__: boxPrototype },
{ value: 3, __proto__: boxPrototype },
];
, getValue , . __proto__ . [[Prototype]] constructor . new .
//
function Box(value) {
this.value = value;
}
// Box()
Box.prototype.getValue = function () {
return this.value;
};
const boxes = [new Box(1), new Box(2), new Box(3)];
new Box(1) Box "" , Box.prototype boxPrototype . Box.prototype . prototype [[Prototype]] . , Object.getPrototypeOf(new Box()) === Box.prototype. Constructor.prototype constructor . , Box.prototype.constructor === Box , .
:
, new . , [[Prototype]] , .
classes .
class Box {
constructor(value) {
this.value = value;
}
// Box.prototype .
getValue() {
return this.value;
}
}
. , Box.prototype . , .
Box.prototype [[Prototype]] , Box.prototype .
function Box(value) {
this.value = value;
}
Box.prototype.getValue = function () {
return this.value;
};
const box = new Box(1);
// , `Box.prototype` .
Box.prototype.getValue = function () {
return this.value + 1;
};
box.getValue(); // 2
, (Constructor.prototype (Constructor.prototype = ...)) .
[[Prototype]] [[Prototype]] . [[Prototype]] .constructor , instance.contructor . constructor , .Constructor.prototype . Function.prototype Constructor.[[Prototype]] . , Object.getPrototypeOf(Constructor) === Function.prototype.
JavaScript [[Prototype]] . ,
// (`__proto__` ) `[[Prototype]]` `Object.prototype` .
const object = { a: 1 };
Object.getPrototypeOf(object) === Object.prototype; // true
// `Array.prototype` `[[Prototype]]` .
const array = [1, 2, 3];
Object.getPrototypeOf(array) === Array.prototype; // true
// RegExp `RegExp.prototype` `[[Prototype]]` .
const regexp = /abc/;
Object.getPrototypeOf(regexp) === RegExp.prototype; // true
, " " .
const array = new Array(1, 2, 3);
const regexp = new RegExp("abc");
, map() " " Array.prototype . .
:
. Object.prototype . Array.prototype.myMethod = function () {...} myMethod .
(monkey patching) . , . , . , SmooshGate , JavaScript " " .
JavaScript (backport). ,Array.prototype.forEach .
, prototype . , Number.prototype 0, Array.prototype , RegExp.prototype /(?:)/.
Number.prototype + 1; // 1
Array.prototype.map((x) => x + 1); // []
String.prototype + "a"; // "a"
RegExp.prototype.source; // "(?:)"
Function.prototype(); // Function.prototype (no-operation, no-op) .
, Map .
Map.prototype.get(1);
// Uncaught TypeError: Map.prototype get
Constructor.prototype Constructor.prototype [[Prototype]] , [[Prototype]] . Constructor.prototype . , Object.getPrototypeOf(Constructor.prototype) === Object.prototype . Object.prototype , [[Prototype]] null. , Object.getPrototypeOf(Object.prototype) === null. , .
function Constructor() {}
const obj = new Constructor();
// obj ---> Constructor.prototype ---> Object.prototype ---> null
, Object.setPrototypeOf() Constructor.prototype [[Prototype]] .
function Base() {}
function Derived() {}
// `Derived.prototype` `[[Prototype]]` `Base.prototype` .
Object.setPrototypeOf(Derived.prototype, Base.prototype);
const obj = new Derived();
// obj ---> Derived.prototype ---> Base.prototype ---> Object.prototype ---> null
, extends .
class Base {}
class Derived extends Base {}
const obj = new Derived();
// obj ---> Derived.prototype ---> Base.prototype ---> Object.prototype ---> null
, Object.create() . prototype constructor , . .
function Base() {}
function Derived() {}
// `[[Prototype]]` `Base.prototype` `Derived.prototype .
// , `Object.setPrototypeOf` .
Derived.prototype = Object.create(Base.prototype);
.
, JavaScript . prototype . ( JavaScript ).
, "console" JavaScript , /Return ( . Firefox Developer Tools, Chrome DevTools Edge DevTools ).
function doSomething() {}
console.log(doSomething.prototype);
// . JavaScript .
// . .
const doSomethingFromArrowFunction = () => {};
console.log(doSomethingFromArrowFunction.prototype);
, doSomething() prototype . .
{
constructor: doSomething(),
[[Prototype]]: {
constructor: Object(),
hasOwnProperty: hasOwnProperty(),
isPrototypeOf: isPrototypeOf(),
propertyIsEnumerable: propertyIsEnumerable(),
toLocaleString: toLocaleString(),
toString: toString(),
valueOf: valueOf()
}
}
:
Chrome [[Prototype]] , . Firefox <prototype> , [[Prototype]] .
, doSomething() .
function doSomething() {}
doSomething.prototype.foo = "bar";
console.log(doSomething.prototype);
:
{
foo: "bar",
constructor: doSomething(),
[[Prototype]]: {
constructor: Object(),
hasOwnProperty: hasOwnProperty(),
isPrototypeOf: isPrototypeOf(),
propertyIsEnumerable: propertyIsEnumerable(),
toLocaleString: toLocaleString(),
toString: toString(),
valueOf: valueOf()
}
}
new doSomething() . new new . new . .
.
function doSomething() {}
doSomething.prototype.foo = "bar"; //
const doSomeInstancing = new doSomething();
doSomeInstancing.prop = "some value"; //
console.log(doSomeInstancing);
.
{
prop: "some value",
[[Prototype]]: {
foo: "bar",
constructor: doSomething(),
[[Prototype]]: {
constructor: Object(),
hasOwnProperty: hasOwnProperty(),
isPrototypeOf: isPrototypeOf(),
propertyIsEnumerable: propertyIsEnumerable(),
toLocaleString: toLocaleString(),
toString: toString(),
valueOf: valueOf()
}
}
}
, doSomeInstancing [[Prototype]] doSomething.prototype. ? doSomeInstancing doSomeInstancing .
doSomeInstancing , doSomeInstancing.[[Prototype]] (doSomething.prototype) . doSomeInstancing.[[Prototype]] doSomeInstancing.[[Prototype]] .
, doSomeInstancing.[[Prototype]] , doSomeInstancing.[[Prototype]].[[Prototype]] . , prototype [[Prototype]] Object.prototype. , doSomeInstancing.[[Prototype]].[[Prototype]] (doSomething.prototype.[[Prototype]](Object.prototype)) .
doSomeInstancing.[[Prototype]].[[Prototype]] , doSomeInstancing.[[Prototype]].[[Prototype]].[[Prototype]] . , doSomeInstancing.[[Prototype]].[[Prototype]].[[Prototype]] , Object.prototype.[[Prototype]] null . , [[Prototype]] , undefined .
.
function doSomething() {}
doSomething.prototype.foo = "bar";
const doSomeInstancing = new doSomething();
doSomeInstancing.prop = "some value";
console.log("doSomeInstancing.prop: ", doSomeInstancing.prop);
console.log("doSomeInstancing.foo: ", doSomeInstancing.foo);
console.log("doSomething.prop: ", doSomething.prop);
console.log("doSomething.foo: ", doSomething.foo);
console.log("doSomething.prototype.prop:", doSomething.prototype.prop);
console.log("doSomething.prototype.foo: ", doSomething.prototype.foo);
.
doSomeInstancing.prop: some value doSomeInstancing.foo: bar doSomething.prop: undefined doSomething.foo: undefined doSomething.prototype.prop: undefined doSomething.prototype.foo: bar
. , .
const o = { a: 1 };
// o Object.prototype [[Prototype]] .
// Object.prototype null .
// o ---> Object.prototype ---> null
const b = ["yo", "whadup", "?"];
// Array.prototype .
// ( indexOf, forEach )
// .
// b ---> Array.prototype ---> Object.prototype ---> null
function f() {
return 2;
}
// Function.prototype .
// ( call, bind ).
// f ---> Function.prototype ---> Object.prototype ---> null
const p = { b: 2, __proto__: o };
// [[Prototype]] __proto__ .
// (Object.prototype.__proto__ ).
// p ---> o ---> Object.prototype ---> null
. __proto__ . Object.prototype.__proto__ , __proto__ Object.create . Object.create .
|
|
IE10 . Object.prototype.__proto__ .
|
JavaScript new .
function Graph() {
this.vertices = [];
this.edges = [];
}
Graph.prototype.addVertex = function (v) {
this.vertices.push(v);
};
const g = new Graph();
// g 'vertices' 'edges' .
// g.[[Prototype]] new Graph() Graph.prototype .
| (IE 5.5 ). , , , JIT . | |
. |
Object.create() . [[Prototype]] .
const a = { a: 1 };
// a ---> Object.prototype ---> null
const b = Object.create(a);
// b ---> a ---> Object.prototype ---> null
console.log(b.a); // 1 (inherited)
const c = Object.create(b);
// c ---> b ---> a ---> Object.prototype ---> null
const d = Object.create(null);
// d ---> null (d null )
console.log(d.hasOwnProperty);
// undefined, d Object.prototype .
. [[Prototype]] , . Object.create(null) .
|
|
| IE8 . Microsoft IE8 . , . . , . |
class Rectangle {
constructor(height, width) {
this.name = "Rectangle";
this.height = height;
this.width = width;
}
}
class FilledRectangle extends Rectangle {
constructor(height, width, color) {
super(height, width);
this.name = "Filled rectangle";
this.color = color;
}
}
const filledRectangle = new FilledRectangle(5, 10, "blue");
// filledRectangle ---> FilledRectangle.prototype ---> Rectangle.prototype ---> Object.prototype ---> null
| . . . | |
| ( ). (transpilers) . |
, Object.setPrototypeOf() [[Prototype]] .
const obj = { a: 1 };
const anotherObj = { b: 2 };
Object.setPrototypeOf(obj, anotherObj);
// obj ---> anotherObj ---> Object.prototype ---> null
. Object.create(null) .
|
|
| . . . . , . IE8 . |
Object.prototype.__proto__ , [[Prototype]] (__proto__ ).
:
Object.prototype.__proto__ . Object.setPrototypeOf .
const obj = {};
// . .
obj.__proto__ = { barProp: "bar val" };
obj.__proto__.__proto__ = { fooProp: "foo val" };
console.log(obj.fooProp);
console.log(obj.barProp);
. __proto__ . .
|
|
. . . IE10 . __proto__ , . Object.setPrototypeOf .
|
, . , .
, , " . itself , hasOwnProperty Object.hasOwn . [[Prototype]] null Object.prototype hasOwnProperty . .
function Graph() {
this.vertices = [];
this.edges = [];
}
Graph.prototype.addVertex = function (v) {
this.vertices.push(v);
};
const g = new Graph();
// g ---> Graph.prototype ---> Object.prototype ---> null
g.hasOwnProperty("vertices"); // true
Object.hasOwn(g, "vertices"); // true
g.hasOwnProperty("nope"); // false
Object.hasOwn(g, "nope"); // false
g.hasOwnProperty("addVertex"); // false
Object.hasOwn(g, "addVertex"); // false
Object.getPrototypeOf(g).hasOwnProperty("addVertex"); // true
: undefined . undefined .
JavaScript , Java C++ . () () Function . "" .
JavaScript new prototype .
[[Prototype]] . , const a1 = new A() , JavaScript( this A() ) a1.[[Prototype]] = A.prototype . , JavaScript , [[Prototype]] . [[Prototype]] . , a1.doSomething, Object.getPrototypeOf(a1).doSomething, Object.getPrototypeOf(Object.getPrototypeOf(a1)).doSomething , Object.getPrototypeOf null . prototype , prototype .
const a1 = new A(); const a2 = new A(); a1.doSomething Object.getPrototypeOf(a1).doSomething , A.prototype.doSomething . , Object.getPrototypeOf(a1).doSomething === Object.getPrototypeOf(a2).doSomething === A.prototype.doSomething.
. , . , JavaScript .
This page was last modified on 2026 10 19 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |