Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the disadvantages of using constructors in js

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "what are the disadvantages of using constructors in js". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the disadvantages of using constructors in js.

1. Instead of inheriting from the prototype chain, it just borrows the constructor, so you cannot inherit the properties and methods of the prototype.

2. Although the properties and methods defined in the constructor are accessible, each instance is copied.

If there are too many examples, too many methods, and a lot of memory, then the method is defined in the constructor, and the reuse of the function is out of the question.

Example

/ / parent constructor function Father () {this.name = 'father' this.speakName1 = function () {console.log (' speakName1')} this.speakName2 = function () {console.log ('speakName2')} this.speakName3 = function () {console.log (' speakName3')} this.speakName4 = function () {console.log () 'speakName4')}} / / method on parent prototype Father.prototype.alertName = function () {console.log (this.name)} / / attribute on parent prototype Father.prototype.age = 21G / child constructor function Children () {Father.call (this)} / / create child instance let C1 = new Children () / / call the prototype method Instance cannot access c1.alertName () / / TypeError: c1.alertName is not a function / / access prototype properties. Console.log (c1.age) / / undefined / / is not defined in the instance to access instance properties, but each instance has its own name value console.log (c1.name) / / father / / to access instance methods, but each instance has its own speakName1 () method, / / and there are too many methods C1.speakName1 () / / speakName1 c1.speakName2 () / / speakName2 c1.speakName3 () / / speakName3 c1.speakName4 () / / speakName4 / / instanceof isPrototypeOf cannot determine the relationship between instance and type console.log (Father.prototype.isPrototypeOf (C1)) / / falseconsole.log (C1 instanceof Father) / / false here I believe that you have a deeper understanding of "what are the disadvantages of using constructors in js", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report