In the previous article, objects and prototypes in JavaScript (I) mentioned some basic operations in the creation of objects in JavaScript, and will continue to discuss them next.
One factory model
We know that to create an object we can use the following code:
var user = new Object(); //Create an object using the new operator user.name = 'Recited on the Triangle Lake'; //Add attributes to the object user.age = 22;user.address = 'Hubei Wuhan';alert(user.name + " " +user.age); //Return 'Recited on the Triangle Lake' Hubei Wuhan'
Creating objects in this way is simple and intuitive, and it is also the most basic method of creating objects in JavaScript. But there is a problem like this, if we need to create multiple objects, then I have to write a lot of duplicate code. For example, if we want to create another object user1, we have to rewrite the above code again, which is inappropriate in the actual development process. In this way, if there are too many objects, the amount of code will increase greatly.
To solve such a problem, we can use a method called factory pattern, which is to solve the problem of instantiating objects producing a large amount of duplicate code.
function create(name, age) { var obj = new Object(); obj.name = name; obj.age = age; obj.run = function () { return this.name +' '+ this.age; }; return obj;}var obj1= create('ZXC', 10); //The first instance var obj2= create('CXZ', 20); //The second instance alert(obj1.run()); alert(obj1.run());From the above code, we can see that the factory pattern solves the problem of large-scale duplication of code during instantiation, but another problem arises, that is, identifying the problem, we can't figure out which object they are. for example
alert(typeof obj1); //Objectalert(obj1 instanceof Object); //true
The above code indicates that box1 is an Object object, but we cannot know which object was created.
Two constructor (construction method)
In order to solve the above problem, we can use constructors to create objects. The only difference between a constructor and a normal function is that the call method is different. However, the constructor is also a function.
function User(name, age) { //Constructor mode this.name = name; this.age = age; this.run = function () { return this.name + ' '+this.age; };}Just use the new operator when creating an object:
var user1= new User('ZXC', 25);var user2= new User('CXZ', 22);Now we can detect whether user1 or user2 belongs to User.
alert(user1 instanceof User);//true
It can be seen that using the constructor method solves the problem of repeated instantiation and also solves the problem of object recognition.
The process of executing when using a constructor is as follows:
1. When executing the new constructor(), the background executes new Object();
2. Scope the constructor to the new object.
3. Execute the code inside the constructor;
4. The background directly returns the new object.
Next, let's take a look at the function problems inside the constructor. If we execute the following statement:
alert(user1.run==user2.run);//The result returns false
The result returns false, which means that the method is actually a reference address. If we create multiple objects repeatedly, the methods in each object will open up new space in memory, which will waste more space. To solve this problem, we need to use the sharing of instance properties or methods. We will continue to discuss solving this problem in the next article.
The above is the objects and prototypes in Javascript introduced to you by the editor (II). I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!