2

I find Node.js implement inherit in this way, for example: https://github.com/nodejs/node/blob/master/lib/_http_server.js

ObjectSetPrototypeOf(ServerResponse.prototype, OutgoingMessage.prototype);   // line 174
ObjectSetPrototypeOf(ServerResponse, OutgoingMessage);  // line 175

instead of only seting prototype:

ObjectSetPrototypeOf(ServerResponse.prototype, OutgoingMessage.prototype);

1 Answer 1

1

So that the static methods of the parent class are inherited by the subclass, because they've made the parent class constructor the prototype of the child class constructor.

Native class syntax does the same thing:

class Parent {
    static example() {
        console.log("Parent.example");
    }
}

class Child extends Parent {
}

console.log(Child.example());                 // "Parent.example"
console.log(Child.hasOwnProperty("example")); // false
console.log("example" in Child);              // true, because it's inherited
console.log(Object.getPrototypeOf(Child) === Parent); // true

Remember that functions are objects. Objects can have prototypes. An object inherits properties from its prototype. In the example above, Child (the function objects) has Parent (the function object) as its prototype, so it inherits the example property. Up through ES5, a function created with JavaScript code always had Function.prototype as its prototype, but starting with ES2015, that's not always true. class syntax makes the superclass constructor the prototype of the subclass constructor. You can also change the prototype of a function after it's created, via Object.setPrototypeOf, but it's usually best to avoid changing the prototype of an existing object.

In Chapter 4 of my new book JavaScript: The New Toys, I use the following example to illustrate this concept (details omitted):

class Color {
}
class ColorWithAlpha {
}

along with this diagram to show the two parallel prototype chains created by that, one for the constructor functions themselves, and the other for the objects they assign as prototype when used to create objects:

enter image description here

6
  • console.log(Child.prototype.prototype); // Got undefined,why it is not Parent.prototype ? May 3, 2020 at 11:25
  • @ZhenchuanRen - Because Child.prototype doesn't have a prototype property (for instance, it isn't a constructor function). You may be confusing the prototype of an object (which you can get via Object.getPrototypeOf) and the prototype property of constructor functions, which is not their prototype, it's the object that's used as the prototype of objects created with the constructor function via new. May 3, 2020 at 11:40
  • You are right. I use console.log(Child.prototype.__proto__); and I got the Parent.prototype. However, I still don't undersatnd why static methods defined in Parent function can be inherited by ObjectSetPrototypeOf(Child, Parent); May 3, 2020 at 11:50
  • @ZhenchuanRen - Functions are objects. Objects can have prototypes. An object inherits properties from its prototype. Up through ES5, a function created with JavaScript code always had Function.prototype as its prototype, but starting with ES2015, that's not always true. class syntax makes the superclass constructor the prototype of the subclass constructor. (You can also change the prototype of a function after it's created, via Object.setPrototypeOf, but it's usually best to avoid changing the prototype of an existing object.) May 3, 2020 at 11:58
  • Thank your help, I think I understand the principle behind this question. Object.setPrototypeOf(Child, Parent) changes Child's __proto__ , instead of changing Child.prototype. So Child could use the static methods in Parent Function. May 3, 2020 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.