| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/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++), class ( class, , ES2015, , JavaScript " " - ).
JavaScript : . , . - , , prototype null. , null .
JavaScript, . , , .
JavaScript "", ( ). -. - , , , , . , .
// 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) -, , .
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'
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.
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().
ECMAScript 5 : Object.create. :
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".
"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:
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();
:
.prototypeObject.create(), 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]] , :
var o = new Foo();
JavaScript - :
var o = new Object();
o.[[Prototype]] = Foo.prototype;
Foo.call(o);
:
o.someProp;
JavaScript , o someProp.
, Object.getPrototypeOf(o).someProp
, Object.getPrototypeOf(Object.getPrototypeOf(o)).someProp .
, . JavaScript-, , . "" JavaScript, - .
This page was last modified on 19 . 2025 . 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 |