In JavaScript, there are many modes for creating an object with custom properties and methods, which are introduced one by one below.
1. Create patterns directly. This is the simplest and most straightforward pattern. First create an object of reference type and then add custom properties and methods to it. The sample code is as follows:
1 var person = new Object();
2 person.name = "Sam";
3 person.age = 16;
4 person.speak = function(){
5 alert(this. name + "is " + this.age + "years old");
6 }
7 person.speak();
You can see that an object of type Object is created above, and then name and age are added to it Properties and a speak method. Although creating a pattern directly is simple, its disadvantage is obvious: when we need to create many identical objects, we have to write code repeatedly every time. In order to solve this problem, we can encapsulate the process of creating objects, so we have the following factory pattern.
2. Factory mode. Factory pattern is a commonly used design pattern in programming. It mainly encapsulates the process of creating objects. The sample code is as follows:
1 function createPerson(name, age){
2 var person = new Object() ;
3 person.name = name;
4 person.age = age;
5 person.speak = function(){
6 alert(this.name + "is " + this.age + "years old");
7 }
8 return person;
9 }
10 var person1 = createPerson("Sam", 16);
11 var person2 = createPerson("Jack", 18);
After using the factory pattern, create objects of the same type It becomes simple. But the factory pattern does not solve the problem of object identification, that is, we cannot determine the specific type of the object created. Developers who have experience in object-oriented programming all know that the creation of objects should be based on classes. Once you have a specific custom class, you can then create objects of that class. Fortunately, in JavaScript, we can simulate a class through the constructor pattern.
3. Constructor pattern. There is no difference between constructors and ordinary functions. Any ordinary function can be used as a constructor, as long as the new operator is used; any constructor can also be called as an ordinary function. But in JavaScript, there is a convention that the function name used as a constructor needs to have the first letter capitalized. The sample code is as follows:
1 function Person(name, age){
2 this.name = name;
3 this.age = age;
4 this.speak = function(){
5 alert(this.name + "is " + this.age + "years old");
6 }
7 }
8 var person1 = new Person("Sam", 16);
9 var person2 = new Person("Jack", 18);
You can see that inside the constructor, we use this to add properties and methods. So, what does this refer to? When we create a Person object, this refers to the created object. Now, we can identify the specific types of objects person1 and person2. After using alert(person1 instanceOf Person), you can find that the output value is true. But the constructor pattern also has its own shortcomings, that is, the methods declared within the constructor will be recreated every time a new object is created (in JavaScript, functions are also objects). In other words, the methods within the constructor are bound to the object, not to the class. The output of the code below can verify our inference.
1 alert(person1.speak == person2.speak); // false
A relatively simple way to solve this shortcoming is to put the function declaration outside the constructor, that is:
1 function Person( name, age){
2 this.name = name;
3 this.age = age;
4 this.speak = speak;
5 }
6 function speak(){
7 alert(this.name + "is " + this.age + "years old");
8 }
9 var person1 = new Person("Sam", 16);
10 var person2 = new Person("Jack", 18);
11 alert(person1. speak == person2.speak); // true
The problem is solved, but this method brings new problems. First, the function speak is declared in the global scope, but it can only be used in the Person constructor. There is a risk of misuse when placed in the global scope; secondly, if a custom type has many methods, Then you need to declare a lot of global functions, which will not only lead to pollution of the global scope, but also is not conducive to code encapsulation. So, is there any way to make a custom type method bound to a class without polluting the global scope? The answer is to use prototype pattern.
4. Prototype mode. After we declare a new function, the function (in JavaScript, functions are also objects) will have a prototype attribute. A prototype is an object that represents the public properties and methods owned by all objects created by this function. The sample code is as follows:
1 function Person(){}
2 Person.prototype.name="Sam";
3 Person.prototype.age=16;
4 Person.prototype.speak = function(){
5 alert(this.name + "is " + this.age + "years old");
6 }
7 var person1 = new Person();
8 person1.speak();
9 var person2 = new Person() ;
10 alert(person1.speak == person2.speak); // true
You can see that although the speak method is not declared in the constructor, the object person1 we created can still call the speak method. This is because JavaScript has A search rule, first search instance attributes and methods, and return if found; if not found, search again in prototype. The prototype pattern makes the method related to the class and does not pollute the global scope, but it also has its own shortcomings: First, all attributes are also related to the class, which means that all objects share one attribute, which is obviously unreasonable. ; Second, there is no way to pass initialization data to the constructor. The solution is simple, just use a mix of constructor pattern and prototype pattern.
5. Combination mode. The sample code is as follows:
1 function Person(name, age){
2 this.name = name;
3 this.age = age;
4 }
5 Person.prototype.speak = function(){
6 alert (this.name + "is " + this.age + "years old");
7 }
8 var person1 = new Person();
9 person1.speak();
10 var person2 = new Person();
11 alert(person1.speak == person2.speak); // true
It is not difficult to find that the combination mode meets all our needs, and it is also a mode that is currently widely used. Developers with experience in object-oriented programming may feel that it is a bit awkward to put the prototype declaration outside the constructor, so can it be put into the constructor? The answer is yes, just use dynamic combination mode.
6. Dynamic combination mode. The principle is to first determine whether a certain attribute or method in the prototype has been declared. If not, declare the entire prototype; otherwise, do nothing. The sample code is as follows:
1 function Person(name, age){
2 this.name = name;
3 this.age = age;
4 if (Person.prototype.speak == "undefined"){
5 Person.prototype.speak = function(){
6 alert(this.name + "is " + this.age + "years old");
7 }
8 }
9 }