Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Javascript inherit parent types with prototype objects

Shulou Source: shulou.com Published: 2022-06-03 10:05:59 09月11日 Update

Editor to share with you how Javascript uses prototype objects to inherit parent types, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article, so let's learn about it!

1. Extract the methods shared by the subclass and make the prototype prototype object of the subclass = new parent class (). The subclass prototype object is equivalent to instantiating the parent class.

Because another space is opened up after the parent class is instantiated, the original parent prototype object will not be affected.

2. Repoint the constructor of the subclass to the constructor of the subclass.

Example

/ / parent constructor function Father (uname, age) {this.name = uname; this.age = age;} Father.prototype.earn = function () {console.log (10000) } / / subconstructor function Son (uname, age, score) {Father.call (this, uname, age); this.score = score;} Son.prototype = new Father (); Son.prototype.constructor = Son; Son.prototype.exam = function () {console.log ("examination");} Var son = new Son ('ldh', 18,100); console.log (son); this is all the content of the article "how Javascript inherits parent types with prototype objects". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Prototypes objects subclasses functions instances articles types content not much most pointing methods more knowledge space industry information information channels channels references Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MariaDB Shulou Information macOS NVidia