JavaScript object-oriented design 2 constructor Mode

Source: Internet
Author: User

We will use the constructor mode to rewrite the factory mode.
Copy codeThe Code is as follows:
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 the Employee is different from the code in the CreateEmployee function, as shown below:
No created object is displayed
Attributes and methods are directly assigned to this object.
No return Statement
To create a new instance of the Employee class, you must use the new operator, which actually goes through four steps:
Create a new object
Assign the scope of the constructor to a new object.
Execute the code in the constructor
Returns a new object.
At the end of the code above, Jim and Sun respectively store two different instances of the Employee. Both instances have a constructor (constructor) attribute that points to the Employee. You can perform the following test:
Copy codeThe Code is as follows:
Alert (Jim instanceof Employee); // true
Alert (Sun instanceof Employee); // true

Both objects are of the Object type and can be detected using the following code.
Copy codeThe Code is as follows:
Alert (Jim instanceof Object); // true
Alert (Sun instanceof Object); // true

Creating a user-defined constructor means that in the future, the instance stone can be regarded as a specific type, which is exactly where the constructor mode is better than the factory mode.
The following describes the constructor in detail:
Use constructor as a function
The only difference between constructors and other functions is that they are called differently. However, constructor is also a function, and there is no special syntax for defining constructor. In fact, any function can be called as a constructor only through the new method. For example, in addition to calling the new method, the Employee can also use the following method.
Copy codeThe Code is as follows:
// Called as a common function
Employee ("Sun", 28, "SoftWare Engineer"); // Add to window
Window. sayName (); // Sun

Copy codeThe Code is as follows: // It is called in the scope of another object.
Var o = new Object ();
Employee. call (o, "Sun", 28, "SoftWare Engineer ");
O. sayName (); // Sum

Constructor Problems
The main problem with using constructor is that it must be re-created on every strength. The sayName method of the above two objects is actually an instance of different functions. You can use the following method to prove it:
Alert (Jim. sayName = Sun. sayName); // false
However, two Function instances are created to complete the same task, so we can rewrite the above Function, as shown below:
Copy codeThe Code is 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 doing the same thing, but the new problem will occur again. The function defined in the global scope can only be referenced by an object, the most terrible problem is that if the object needs to define many methods, many global functions need to be defined. Therefore, there is no encapsulation for this custom reference type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.