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 ways to implement JavaScript to write classes

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail the ways to write classes in JavaScript, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

There are many ways to realize javascript class writing on the Internet, which can be summarized as follows. I summed it up by myself.

Construction method mode; prototype mode; construction method + prototype mixed mode

Now let's analyze the advantages and disadvantages of the above methods in detail:

Construction method and mode

This is the most basic and most similar way for Java to write class. The above code:

/ / create a Student class function Student (name) {this.name = name; this.sayName = function () {alert (this.name);};} / / new two different Student. Var jimmy = new Student ('jimmy'); var henry = new Student (' henry'); jimmy.sayName (); / / display jimmy henry.sayName (); / / display henry

This method is simple and in line with JAVAer's taste, but for every new object in new, a sayName method is allocated in memory, which does not perform very well.

Prototype mode

/ / create a Student class / / both properties and methods use Student.prototype to set function Student (name) {Student.prototype = name; Student.prototype.sayName = function () {alert (this.name);}} / / new two different Student. Var jimmy = new Student ('jimmy'); var henry = new Student (' henry'); jimmy.sayName (); / display henrybirds! Henry.sayName (); / / display henryproof!

Maybe the code executed is different from the expectations of some children's shoes. Both times the alert pops up henry! In fact, it is easy to understand. Properties and methods are set through prototype. The same property or method of different objects points to the same memory, so henry is set after jimmy. So henry overwrites jimmy.

Mixed mode

The construction method can allocate different memory for each object of the same class, which is very suitable for setting properties when writing the class, but when setting the method, we need to make different objects of the same class share the same memory. The writing method is written in the form of a prototype. So when writing a class, you need to mix the construction method with the prototype. Cut the crap and look at the code:

/ / create a Student class / / property through constructor setting / / method to set function Student (name) {this.name = name; Student.prototype.sayName = function () {alert (this.name);}} / / new two different Student via Student.prototype. Var jimmy = new Student ('jimmy'); var henry = new Student (' henry'); jimmy.sayName (); / / display jimmy henry.sayName (); / / display henry

So far so good. For different objects of the same class, the attributes occupy the memory and the methods share the same memory. In fact, there is still a small problem:

For each new object in new, it is executed once

Student.prototype.sayName = function () {alert (this.name);}

Cause unnecessary repetition of operations. You can set a flag bit in the class, set the flag bit to true the first time it is executed, and no longer set the method for prototype if it is true.

On the implementation of JavaScript writing classes which way to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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