Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use constructor in Javascript

Shulou Source: shulou.com Published: 2022-05-31 10:37:09 09月25日 Update

This article focuses on "how to use constructor in Javascript". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use constructor in Javascript.

Description

1. Constructor is called a constructor because it refers to the constructor itself.

2. Constructor is mainly used to record which constructor the object is referenced to, so that the prototype object can be redirected to the original constructor.

In many cases, we need to manually use the constructor property to restore the original constructor.

Example

Function Star (uname, age) {this.uname = uname; this.age = age;} / / in many cases, we need to manually use the attribute constructor to refer back to the original constructor / / Star.prototype.sing = function () {/ / console.log ('I can sing'); / /} / / Star.prototype.movie = function () {/ / console.log ('I can do a movie') / /} Star.prototype = {/ / if we modify the original prototype object and assign a value to the prototype object, we must manually use constructor to refer back to the original constructor constructor: Star, sing: function () {console.log ('I can sing') }, movie: function () {console.log ('I can act in a movie');}} var ldh = new Star ('Andy Lau', 18); var zxy = new Star ('Jacky Cheung', 19); console.log (Star.prototype); console.log (ldh.__proto__) Console.log (Star.prototype.constructor); console.log (ldh.__proto__.constructor); now that you have a better understanding of "how to use constructor in Javascript", 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!

Tags: Functions objects prototypes manual content properties situations movies learning original practical deeper interest can make examples practicality practical Jacky Cheung pointing easy to operate Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Huawei Docker Shulou Information Shulou Technology