Home>Article>Web Front-end> Javascript creates classes and objects (graphic tutorial)

Javascript creates classes and objects (graphic tutorial)

亚连
亚连 Original
2018-05-19 10:49:07 1733browse

There are many ways to create classes and objects using Javascript. Let’s list them now: 1. Original creation method, 2. Factory method pattern, 3. Constructor method pattern, 4. Dynamic prototype method. Let's take a detailed look at examples through examples

Now we summarize several methods of creating classes and objects in Javascript:

1. Original creation method:

Original The creation method is difficult to accept for people who are familiar with object-oriented. It always feels that the encapsulation of attributes and methods is not very tight. This encapsulation is carried out in the form of "object name" and ".", which means that the attributes and methods followed by the object name are this What the object owns, this object (for example: person) is the encapsulated result. You can continue to add methods and attributes, for example, add the age attribute: person.age=23; This creation method will make people familiar with Java programming feel very Uncomfortable. We can further "encapsulate" the original creation method. Please see the next step:

2. Factory method pattern:

The factory method pattern looks more like a class. personFactory is The original creation method is encapsulated and returns the created object to the person reference variable. Person can then reference the created object, but it is not perfect yet: you create an object every time and use the object to call show( ) method, a new show() function will be created. They can call the same show method. The optimization method is to put show outside the factory, as follows:

Functionally speaking, the above code solves It solves the problem of function reuse, but the presentation method is not like creating an object. People who are familiar with Java still feel uncomfortable. Please see the next step:

3. Constructor mode:

The creation method of the above code is almost the same as the creation method of Java classes and objects. It encapsulates the properties and methods of the class. Then use the new keyword to create and return an object. Isn't this the process of creating classes and objects in Java? Yes, it is this process, but it can also be optimized. The object created in this way will be created immediately when the show method is called. For a show function, can we create a method common to all objects? Just like static methods in Java classes, all objects use the same static method, and the answer is yes. Please see the next step:

4. Dynamic prototype method:

A little trick is used here. When using new to create an object, the if judgment statement in the Person function block will be executed, and the order is from Going down, of course the tag variable is not defined at the beginning, so the content in the if statement block is executed:

Person.prototype.show=function(){ document.write(this.name+" "+this.age+" "+this.sex); document.write("
"); }

The meaning of this content is to create a show method belonging to the Person class. Note that it is a class method. , equivalent to the static modified method in Java, rather than the method of a single object, so that all objects can call the same method, and there is no need to create your own show function every time a different object calls the method, which saves money. Wouldn't this method be better as it saves space and time? Let me explain here, the properties and methods constructed in the "class name.prototype.property/method" method are equivalent to the variables or methods modified with static in Java. They belong to the entire class, not a single object, that is, all objects are shared.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

What BUGs and errors often occur in JS

Summary of parent-child mutual communication methods in VueJs components

Detailed explanation of JS cross-domain POST implementation steps

The above is the detailed content of Javascript creates classes and objects (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn