Confused about JavaScript prototypal inheritance

In the book " JavaScript Ultimate Guide 5th Edition, " section 9.2, "Prototypes and Inheritance," I find the following words:

In the previous section, I showed that the new operator creates a new, empty object, and then calls the constructor as a method of this object. This is not a complete story, however. After creating an empty object, a new one sets up a prototype of this object. The prototype of an object is the value of the prototype property of its constructor. All functions have a prototype property that is automatically created and initialized when the function is defined. The initial value of the prototype property is an object with one property. This property is a named constructor and refers to the constructor function with which the prototype is associated. (You can call the constructor property from Chapter 7; that’s why every object has a constructor property.) Any properties that you add to this prototype object will appear to be properties of objects initialized by the constructor.

Now, if this is true, how can prototype inheritance exist? I mean, suppose a prototype function constructor object initially has a constructor property. Since the prototype object itself is an object, we often use it to define its constructor prototype_object.constructor. But now it prototype_objectalready has a property constructorand points to the constructor function with which the prototype is associated . In this situation, how does inheritance exist?

+3
source share
4 answers

Say a dog is a mammal.

function Mammal() {
  this.milk = true;
};

function Dog() { this.bark = true; } Dog.prototype = new Mammal;

, Dog . Mammal , , Dog , JavaScript , , Mammal Mammal (), Dog Dog.

Dog.prototype Mammal ( Mammal, ). BUT Dog - Dog. , ; , . JS Dog.prototype, JS Mammal.prototype ( , ).

, .

+8

.constructor JavaScript. .

, -, , , - :

const myCar = new Racecar();
console.log(myCar.constructor); // [Function: Racecar]

const car2 = new myCar.constructor();
console.log(car2.constructor); // [Function: Racecar]

, .constructor . , .constructor , JavaScript, , .

, .constructor , - - . , , - - JavaScript. , ( ), JavaScript, .

, - , - -. , .

instanceof .constructor. , .prototype ( ) .

.constructor ( ). , . , ES5:

function Car () {}

console.log(Car.prototype.constructor); // Car

function Racecar () {}

Racecar.prototype = Object.create(Car.prototype);
// To preserve the same relationship we have with the Car
// constructor, we'll need to reassign the .prototype.constructor:
Racecar.prototype.constructor = Racecar;

var myCar = new Racecar();
console.log(myCar.constructor); // [Function: Racecar]

ES6 :

// ES6
class Car {}
class Racecar extends Car {}

const myCar = new Racecar();
console.log(myCar.constructor); // [Function: Racecar]

, - ES6, .constructor. ? factory , , , . . "Factory vs vs " .

+14

obj, obj.prototype, obj obj.prototype.constructor.

obj.prototype . , proto = obj.prototype, proto proto.prototype.constructor.

, obj.prototype.prototype.constructor, obj.prototype.constructor.

+1

Source: https://habr.com/ru/post/1730981/


All Articles