In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how js implements copy inheritance. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The parent class code is as follows:
/ / define an animal class function Animal (name) {/ / attribute this.name = name | | 'Animal'; / / instance method this.sleep = function () {console.log (this.name +' sleeping!') ;}} / / prototype method Animal.prototype.eat = function (food) {console.log (this.name + 'eating:' + food);}; copy inheritance function Cat (name) {var animal = new Animal (); for (var p in animal) {Cat.prototype [p] = animal [p] } / / the following implementation modifies the prototype object, which will cause a single instance to modify name, which will affect the name value of all instances / / Cat.prototype.name = name | | 'Tom'; incorrect statement, the next sentence is the correct implementation this.name = name | |' Tom';} / / Test Codevar cat = new Cat (); console.log (cat.name); console.log (cat.sleep ()); console.log (cat instanceof Animal) / / falseconsole.log (cat instanceof Cat); / / true
Features:
Support for multiple inheritance
Disadvantages:
Low efficiency and high memory footprint (because you want to copy the properties of the parent class)
Cannot get methods that cannot be enumerated in the parent class (methods that cannot be enumerated, cannot be accessed using for in)
Thank you for reading! This is the end of this article on "how to achieve copy inheritance in js". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.