Constructor mode for javascript Object creation (2)

Source: Internet
Author: User

Code rewriting for the factory mode in the previous chapter copy code 1 function Human (name, sex) {2 this. name = name; 3 this. sex = sex; 4 this. say = function () {5 alert (this. name); 6} 7} 8 var man = new Human ("", "male"); 9 var woman = new Human (" ", "female"); copy the code to see that there is no Human. his first letter is capitalized. this is a syntax convention. A constructor must always start with an upper-case letter. A non-constructor must start with a lower-case letter to distinguish between constructor and non-constructor. to create a Human instance, you must use the new operator. the following four steps are displayed: 1. create a new object or variable. 2. assign the scope of the constructor to a new variable or object. 3. execute the code in the constructor 4. returns a new object. 1 alert (man instanceof Object); // true2 alert (man instanceof Human); // true3 alert (woman instanceof Object); // true4 alert (woman instanceof Human ); // true here, true is a good example: creating a custom constructor means that its instance can be expressed as a specific type in the future. this is where the Korean mode is superior to the factory mode. however, constructor still has its own defects, or its shortcomings. both man and woman have a method named "say ()". Their functions are the same. however, these two methods are not created by the same Function () instance. 1 function Human (name, sex) {2 this. name = name; 3 this. sex = sex; 4 this. say = new Function ("alert (this. name); "); 5} this means that if you need n instances, you need to create n say Function () instances. this will cause a waste of resources. continue to optimize the above Code, so that this say () method is created only once. share a reference. copy code 1 function Human (name, sex) {2 this. name = name; 3 this. sex = sex; 4 this. say = say; 5} 6 function say () {7 alert (this. name); 8} copy the code. Because say contains a pointer to the function, the Human instance shares the same say () function in the global scope. however, this is unacceptable, because if the system is large, there will be many global variables, and the danger of getting rid of them will arise if you are not careful. the encapsulated objects are no longer encapsulated. then we have the prototype. the next chapter describes the prototype mode.

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.