Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the points for attention for js to create a constructor?

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

This article is to share with you what are the points for attention in creating constructors in js. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. The first letter of the constructor name needs to be capitalized.

2. The constructor does not need return to return the result.

3. You must use new to call the constructor.

4. Just call the function to create the object.

5. This must be added before properties and methods.

Example

/ / Constructor (generally refers to a certain kind of thing) function Star (name,age,sex) {this.name=name;this.age=age;this.sex=sex;this.sing = function (sang) {console.log (sang);}} / / object (something specific) var ldh = new Star ('Andy Lau', 18 'male') / / call the function and return an object var zxy = new Star ('Jacky Cheung', 19, 'male'); console.log (ldh.name); console.log (ldh ['sex']); ldh.sing (' ice rain'). Thank you for reading! This is the end of this article on "what are the points for js to create a constructor?". 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 for more people to see!

Tags: Functions objects things content more articles good practical ice rain capitalization letters examples attributes Zhang Xueyou articles methods see knowledge results Andy Lau Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Redmi Shulou Technology Shulou Information MySQL