We will rewrite the factory pattern using the constructor pattern.
function Employee(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
this.sayName = function () {
alert(this.name);
} ;
}
var Jim = new Employee("jim", 22, "SoftWare Engineer");
var Sun = new Employee("Sun", 24, "Doctor");
Jim .sayName();
Sun.sayName();
In the above code, the Employee function replaces the CreateEmployee function. The code in Employee is different from the code in CreateEmployee as follows:
None The displayed created object
directly assigns the properties and methods to this object
without a return statement
To create a new instance of the Employee class, you must use the new operator, which actually involves four steps:
Create a new object
Assign the scope of the constructor to the new object
Execute the code in the constructor
Return the new object
Finally, Jim and Sun in the above code save two different Employee values. Instance, both instances have a constructor attribute, which points to Employee. You can do the following test
alert(Jim instanceof Employee); //true
alert(Sun instanceof Employee);//true
These two objects at the same time They are all of type Object and can be detected through the following code.
alert(Jim instanceof Object); //true
alert(Sun instanceof Object);//true
Creating a custom constructor means that in the future, its instance can be marked as a specific type, and this is also the constructor Where patterns outperform factory patterns.
The following explains the constructor in detail:
Treat the constructor as a function
The only difference between the constructor and other functions is the way to call them. But constructors are also functions, and there is no special syntax for defining constructors. In fact, any function can only be called through new, then it can be used as a constructor. For example, in addition to calling Employee using the new method above, it can also be called in the following ways.
//Call
Employee(" as a normal function" Sun", 28, "SoftWare Engineer"); //Add to window
window.sayName();//Sun
//Call in the scope of another object
var o = new Object();
Employee.call(o , "Sun", 28, "SoftWare Engineer");
o.sayName();//Sum
Problems with constructors
The main problem with using constructors is that Create it again on each strength. The sayName methods in the above two objects are actually instances of different Functions. This can be proved by the following method:
alert(Jim.sayName == Sun.sayName);//false
But create two Function instances that complete the same task, so we rewrite the above function as follows
function Employee(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
this.sayName =sayName;
}
function sayName() {
alert(this.name);
}
This solves the problem of two functions The same problem, but new problems will arise again. This function defined in the global scope can actually only be referenced by a certain object, and the most terrible problem is that if the object needs to define many methods, then It is necessary to define many global functions, so this custom reference type has no encapsulation at all.