Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of js prototype unable to pass parameters

Shulou Source: shulou.com Published: 2022-06-03 08:00:38 10月01日 Update

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!

Tags: Types examples functions prototypes examples analysis content parameters articles constructors differences two values examples interests guys partners data more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft vpn OPPO Reno Shulou Tech Info