| The code is as follows |
Copy Code |
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", "SoftWare Engineer"); var sun = new Employee ("Sun", "Doctor"); Jim.sayname (); Sun.sayname (); |
In the above code, the employee function replaces the Createemployee function, and the code in employee differs from the code in Createemployee as follows:
Create object not shown
Assign properties and methods directly to the This object
No return statement
To create a new instance of the employee class, you must use the new operator to actually go through four steps:
Create a new object
Assigning the scope of a constructor to a new object
Executing code in constructors
return new Object
The above code finally, Jim and Sun are saving two different instances of employee, each of which has a constructor (constructor) attribute, which points to employee and can be tested as follows
| The code is as follows |
Copy Code |
Alert (Jim instanceof Employee); True Alert (Sun instanceof Employee);//true
|
Both of these objects are of type object, and can be detected by the following code.
| The code is as follows |
Copy Code |
Alert (Jim instanceof Object); True Alert (Sun instanceof Object);//true
|
Creating a custom constructor means that its instance can be labeled as a specific type in the future, which is where the constructor pattern is better than the factory pattern.
The following is a detailed explanation of the constructor:
To use a constructor as a function
The only difference between constructors and other functions is that they are invoked in a different way. But constructors are also functions, and there is no special syntax for defining constructors. In fact, any function, which is invoked only through new, can be used as a constructor, for example, in addition to the above invocation by the new method, the employee can be invoked in the following way.
Called as a normal function
| The code is as follows |
Copy Code |
Employee ("Sun", "SoftWare Engineer"); Add to Window Window.sayname ();//sun
|
Called in the scope of another object
| The code is as follows |
Copy Code |
var o = new Object (); Employee.call (O, "Sun", "SoftWare Engineer"); O.sayname ();//sum
|
Problems with constructors
The main problem with constructors is to recreate each of the strengths, the Sayname method in the above two objects is actually an instance of a different function, which can be proved by the following methods:
Alert (Jim.sayname = = sun.sayname);//false
But create two function instances to accomplish the same task, so we rewrite the above functions as follows
| The code is as follows |
Copy Code |
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 doing the same thing, but the new problem comes up again, and the function defined in the global scope can actually only be referenced by an object, and the most important problem is that if the object needs to define many methods, it needs to define many global functions