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

Example Analysis of js prototype unable to pass parameters

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis that the js prototype can not pass parameters, the article is very detailed, has a certain reference value, interested friends must read it!

1. Because the prototype contains problems caused by referencing type values, in the case of creating a subtype, parameters cannot be passed like the constructor of a super type.

2. The constructor parameters passed to the supertype will change all instances that inherit the supertype subtype.

Example

/ / parent constructor function Father (name, age) {this.age = age this.child = {name: name}} / / child constructor function Children () {} Children.prototype = new Father ('father' Create subinstance let C1 = new Children () let c2 = new Children () / / modify reference type value namec1.child.name = 'modify reference type value' / / modify value type agec1.age = 222console.log (c1.age) / / 222console.log (c1.child) / / {name: 'modify reference type value'} console.log (c2.age) / / 111console.log (c2.child) / / {name: 'modify reference type value'}

After you modify the value type age of an instance, the value type age values of the two instances are different. On the contrary, after you modify the reference type value of an instance, the reference type value of all instances changes, there is no isolation, and a single data is shared.

The above is all the contents of the article "sample Analysis of js prototypes cannot be passed parameters". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report