[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain [Back]  [Original]

- JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

JavaScript , (Java C++ ) .

, JavaScript . . , null . null , . JavaScript (static dispatching) .

JavaScript , . , classes .

JavaScript , . , .

In this article

JavaScript "" ( ) . , , .

: ECMAScript someObject.[[Prototype]] someObject . [[Prototype]] Object.getPrototypeOf() Object.setPrototypeOf() . JavaScript __proto__ . obj.__proto__ , obj.[[Prototype]] . Object.getPrototypeOf(obj) .

[[Prototype]] func.prototype . prototype .

[[Prototype]] , . , __proto__ . { __proto__: ... } , obj.__proto__ . { a: 1, b: 2, __proto__: c } , c(null ) [[Prototype]] . , a b . [[Prototype]] " " , .

.

js
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 .

, , .

js
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 .

js
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 '' . .

js
const boxes = [
  { value: 1, getValue() { return this.value; } },
  { value: 2, getValue() { return this.value; } },
  { value: 3, getValue() { return this.value; } },
];

, . , getValue [[Prototype]] .

js
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 .

js
//  
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 .

js
class Box {
  constructor(value) {
    this.value = value;
  }

  //  Box.prototype .
  getValue() {
    return this.value;
  }
}

. , Box.prototype . , .

Box.prototype [[Prototype]] , Box.prototype .

js
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]] . ,

js
//   (`__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

, " " .

js
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 /(?:)/.

js
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 .

js
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. , .

js
function Constructor() {}

const obj = new Constructor();
// obj ---> Constructor.prototype ---> Object.prototype ---> null

, Object.setPrototypeOf() Constructor.prototype [[Prototype]] .

js
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 .

js
class Base {}
class Derived extends Base {}

const obj = new Derived();
// obj ---> Derived.prototype ---> Base.prototype ---> Object.prototype ---> null

, Object.create() . prototype constructor , . .

js
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 ).

js
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() .

js
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 . .

.

js
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 .

.

js
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

. , .

js
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__
. __proto__ . Object.prototype.__proto__ , __proto__ Object.create . Object.create .
IE10 . Object.prototype.__proto__ .

JavaScript new .

js
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

Object.create() . [[Prototype]] .

js
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   .
Object.create
. [[Prototype]] , . Object.create(null) .
IE8 . Microsoft IE8 . , . . , .

class

js
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()

, Object.setPrototypeOf() [[Prototype]] .

js
const obj = { a: 1 };
const anotherObj = { b: 2 };
Object.setPrototypeOf(obj, anotherObj);
// obj ---> anotherObj ---> Object.prototype ---> null
Object.setPrototypeOf
. Object.create(null) .
. . . . , . IE8 .

__proto__

Object.prototype.__proto__ , [[Prototype]] (__proto__ ).

: Object.prototype.__proto__ . Object.setPrototypeOf .

js
const obj = {};
//   .   .
obj.__proto__ = { barProp: "bar val" };
obj.__proto__.__proto__ = { fooProp: "foo val" };
console.log(obj.fooProp);
console.log(obj.barProp);
__proto__
. __proto__ . .
. . . IE10 . __proto__ , . Object.setPrototypeOf .

, . , .

, , " . itself , hasOwnProperty Object.hasOwn . [[Prototype]] null Object.prototype hasOwnProperty . .

js
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 .


Web Proxy Viewer  |  New URL  |  Original Page