Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the advantages of js prototype chain inheritance

Shulou Source: shulou.com Published: 2022-06-03 02:17:10 10月02日 Update

This article introduces the relevant knowledge of "what are the advantages of js prototype chain inheritance". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Very pure inheritance relationship. An instance is an instance of a subclass and a parent class.

2. Take the instance of the parent class as the prototype of the subclass, and the parent class can access the new prototype methods / properties.

3. Simple and easy to implement.

Example

Function Cat () {}; var tom=new Cat (); function Animal () {} Animal.prototype.eat= function (food) {console.log ("I eat" + food) {var cat=new Animal (); Cat.prototype=cat;var kitty=new Cat (); kitty.eat ("fish"); console.log (tom instanceof Cat); / / falseconsole.log (tom instanceof Animal); / / falseconsole.log (kitty instanceof Cat); / / trueconsole.log (kitty instanceof Animal) / / this is the end of true's "what are the advantages of js prototype chain inheritance". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Prototypes examples advantages content subclasses more knowledge practicality achievement and then dilemmas reality attributes situations articles methods cases editing websites industries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi Shulou Technology MySQL Microsoft