[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/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++), class ( class, , ES2015, , JavaScript " " - ).

JavaScript : . , . - , , prototype null. , null .

JavaScript, . , , .

In this article

JavaScript "", ( ). -. - , , , , . , .

js
//    someObject.[[Prototype]]   someObject.
//    (   ECMAScript).
//        .

// ,     'o'    a  b
// {a:1, b:2}

// o.[[Prototype]]   b  
// {b:3, c:4}

// , o.[[Prototype]].[[Prototype]]  null
// null -     
//  , null    [[Prototype]]

//       :
// {a:1, b:2} ---> {b:3, c:4} ---> null

console.log(o.a); // 1
//     'o'   'a'?
// ,     1

console.log(o.b); // 2
//     'o'   'b'?
// ,     2.
//   o.[[Prototype]]    'b',
//         .
//    "property shadowing" ( )

console.log(o.c); // 4
//     'o'   ''?
// ,     .
//     o.[[Prototype]]   ''?
// ,   4

console.log(o.d); // undefined
//     'o'   'd'?
// ,     .
//     o.[[Prototype]]   'd'?
// ,     .
// o.[[Prototype]].[[Prototype]]  null,  ,
//   ,  undefined

, . , getter setter.

""

JavaScript "" , . JavaScript . , , " " (property shadowing), ( - method overriding).

this(/ru/docs/Web/JavaScript/Reference/Operators/this) -, , .

js
var o = {
  a: 2,
  m: function () {
    return this.a + 1;
  },
};

console.log(o.m()); // 3
//      'o.m' this   'o'

var p = Object.create(o);
// 'p' -  'o'

p.a = 12; //    'a'  'p'
console.log(p.m()); // 13
//   'p.m' this   'p'.
// ..  'p'   'm'  'o',
// this.a  'p.a',   'a'  'p'

js
var o = { a: 1 };

//   'o'  Object.prototype    [[Prototype]]
//  'o'    'hasOwnProperty'
// hasOwnProperty     Object.prototype.
//   'o'  hasOwnProperty  Object.prototype
// Object.prototype     null.
// o ---> Object.prototype ---> null

var a = ["yo", "whadup", "?"];

//    Array.prototype
// (    ,  indexOf, forEach  ..).
//      :
// a ---> Array.prototype ---> Object.prototype ---> null

function f() {
  return 2;
}

//    Function.prototype
// (    ,  call, bind  ..):
// f ---> Function.prototype ---> Object.prototype ---> null

JavaScript "" "" , new.

js
function Graph() {
  this.vertexes = [];
  this.edges = [];
}

Graph.prototype = {
  addVertex: function (v) {
    this.vertexes.push(v);
  },
};

var g = new Graph();
//  'g'    'vertexes'  'edges'.
// g.[[Prototype]]   Graph.prototype   new Graph().

Object.create

ECMAScript 5 : Object.create. :

js
var a = { a: 1 };
// a ---> Object.prototype ---> null

var b = Object.create(a);
// b ---> a ---> Object.prototype ---> null
console.log(b.a); // 1 ()

var c = Object.create(b);
// c ---> b ---> a ---> Object.prototype ---> null

var d = Object.create(null);
// d ---> null
console.log(d.hasOwnProperty);
// undefined, .. 'd'    Object.prototype

class

ECMAScript 6 , . , , , . JavaScript - . : "class", "constructor", "static", "extends" "super".

js
"use strict";

class Polygon {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

class Square extends Polygon {
  constructor(sideLength) {
    super(sideLength, sideLength);
  }
  get area() {
    return this.height * this.width;
  }
  set sideLength(newLength) {
    this.height = newLength;
    this.width = newLength;
  }
}

var square = new Square(2);

, , (performance), . , .

, , .

, , - , hasOwnProperty, Object.prototype.

hasOwnProperty JavaScript , .

: , undefined. , undefined.

:

Object.prototype .

monkey patching . , , , Prototype.js, , "" .

- (, Array.forEach) -.

B A:

js
function A(a) {
  this.varA = a;
}

// What is the purpose of including varA in the prototype when A.prototype.varA will always be shadowed by
// this.varA, given the definition of function A above?
A.prototype = {
  varA: null, // Shouldn't we strike varA from the prototype as doing nothing?
  // perhaps intended as an optimization to allocate space in hidden classes?
  // https://developers.google.com/speed/articles/optimizing-javascript#Initializing instance variables
  // would be valid if varA wasn't being initialized uniquely for each instance
  doSomething: function () {
    // ...
  },
};

function B(a, b) {
  A.call(this, a);
  this.varB = b;
}
B.prototype = Object.create(A.prototype, {
  varB: {
    value: null,
    enumerable: true,
    configurable: true,
    writable: true,
  },
  doSomething: {
    value: function () {
      // 
      A.prototype.doSomething.apply(this, arguments); // call super
      // ...
    },
    enumerable: true,
    configurable: true,
    writable: true,
  },
});
B.prototype.constructor = B;

var b = new B();
b.doSomething();

:

  • .prototype
  • Object.create()

prototype Object.getPrototypeOf

, JavaScript Java C++, "" . , - . "classes", , .

, function A prototype. new. - [[Prototype]] . , var a1 = new A(), JavaScript ( function A()) a1.[[Prototype]] = A.prototype. , , JavaScript , . , [[Prototype]]. , prototype, . prototype .

[[Prototype]] , :

js
var o = new Foo();

JavaScript - :

js
var o = new Object();
o.[[Prototype]] = Foo.prototype;
Foo.call(o);

:

js
o.someProp;

JavaScript , o someProp. , Object.getPrototypeOf(o).someProp , Object.getPrototypeOf(Object.getPrototypeOf(o)).someProp .

, . JavaScript-, , . "" JavaScript, - .


Web Proxy Viewer  |  New URL  |  Original Page