There are a number of ways to create objects in Javascript.
Object constructor/literal number :
Aside from the design pattern, the most basic method is to call the object constructor to create one and then add the properties to the object.
Copy Code code as follows:
var student = new Object ();
Student.name = "Xiao Ming";
Student.age = 20;
Student.getname = function () {
alert (this.name);
}
Students who are familiar with JavaScript object literals can change to a better way of writing, at least to look more concise.
Copy Code code as follows:
var student = {
Name: "Xiao",
Age:18,
Getname:function () {
alert (this.name);
}
};
Disadvantage: The disadvantage of the above approach is that when you create many similar objects using the same interface, you generate a lot of duplicate code. This should be easy to understand, the function (method or class) is generally used to create a public method, the above object creation process, there is no function shadow, so no reuse.
Factory mode :
The factory pattern abstracts the process of creating objects specifically. Just like a black box, you just call the function (enter the factory), and pass in the corresponding parameters (various raw materials), will come out a corresponding object (factory produced products). The factory pattern solves the problem of creating multiple similar objects.
Copy Code code as follows:
function Studentfactory (name,age) {
var student = new Object ();
Student.name = name;
Student.age = age;
Student.sayname = function () {
alert (this.name);
}
return student;
}
var P1 = studentfactory ("Ming", 20);
var P2 = studentfactory ("the", 18);
Disadvantages: The factory model also has shortcomings, the biggest disadvantage is the object type identification problem. You can only determine that the object is of type Object (P1 instanceof object), and you cannot specifically determine what type it is. The student created using the factory pattern actually have similar properties and methods, but the values are different. A better solution is to create a Student function so that all objects belong to the Student type. So the factory model is not bad, but the constructor mode is more excellent.
Constructors for custom types:
Constructors can be used to create objects of a particular type.
Copy Code code as follows:
function Student (name,age) {
THIS.name = name;
This.age = age;
This.sayname = function () {
alert (this.name);
}
}
var p3 = new Student ("Ming", 20);
var P4 = new Student ("", 18);
Alert (P3 instanceof Student);
alert (p3.sayname==p4.sayname); False
Disadvantages: The disadvantage of the custom constructor is that each object will re-create its own method, which is the same (like Sayname), but they are not the same (P3.sayname and p4.sayname are not equal).
Prototype mode:
Define an empty function, and then add all the properties and methods to the prototype so that all objects share these properties and methods.
Copy Code code as follows:
function Student () {};
Student.prototype.name = "Ming";
Student.prototype.age = 20;
Student.prototype.friends = [' Qi '];
Student.prototype.sayName = function () {
alert (this.name);
};
Disadvantages: Some properties cannot be shared, and shared back brings problems, such as: friends. Most of the friends of each classmate are not the same.
Combination of constructors and prototypes:
Copy Code code as follows:
function Student (name, age, Friends) {
THIS.name = name;
This.age = age;
This.friends = friends;
}
Student.prototype = {
Constructor:student,
Sayname:function () {
alert (this.name);
}
};
Summary: The combination of constructors and prototypes is a widely recognized method for creating custom types. is also the best method in the above methods.
/*************************************************************************************************************/
In fact, there are many ways to create objects, but it is possible that some of the more specific scenarios, need to continue the optimization.
Dynamic Prototyping mode:
It is an optimization of the combination of constructors and prototypes. For those shared properties and methods, if initialized, there is no need to repeat the initialization to improve efficiency.
Copy Code code as follows:
function Student (name, age) {
THIS.name = name;
This.age = age;
if ((typeof this.sayname)!= "function") {
Student.prototype.sayName = function () {
alert (this.name);
}
}
}
var stu = new Person ("Ming", 20);
Alert (Stu instanceof Student);
Stu.sayname ();
var stunew = new Person ("", 18);
Alert (stunew instanceof Student);
Stunew.sayname ();
When multiple student objects are created, the Sayname method is initialized only once.
Finally, there is a very useful way to create objects, that is, to secure the constructor.
Secure constructor Pattern:
This and new are prohibited in this mode, and all objects have no public properties. You can only read the value of a variable, not modify it.
Copy Code code as follows:
Secure constructor Mode
function Student (name, age) {
var o = new Object ();
O.sayname = function () {
alert (name);
}
return o;
}
var stu = Student ("Ming", 21);
Stu.sayname ();
The above summary of several common JavaScript to create a custom object method, very comprehensive, if you have better, please contact me, this article continues to update.