JS Object-Oriented class

Source: Internet
Author: User

Class: In fact, there can be 4 mainstream models to achieve, but each has advantages and disadvantages, the final combination of the model wins.

1. Factory mode: The origin is the use of this mode we can reuse a lot of code, class, it is to achieve the world of all things reuse template is it. Implementation is simply to declare an object in a method, add the object's properties and methods, and then return the object. Pros: Implement code reuse; Disadvantage: You cannot discriminate which class the object belongs to.

2. Constructor mode: First capitalize the default method name, declaring that you want to use new; For example: var user = new Person (); The most critical element of the constructor is the properties and methods we want to use the This keyword, such as

THIS.name = name, so that when the constructor method is called, we assign each object a separate property method. The disadvantage is that when there is a method in the class we write to This.sayname = function () {alert (name)} This time is not good, because each object will have a method, will rewrite n methods, they are function () {alert (name)}, What a waste of memory ah. The type of conjecture method is function, which belongs to the reference type, and the location should be different from the simple property name, so the memory is wasted. Pros: Implement code reuse, and we can judge which class belongs to by instanceof; disadvantage: Write a method to create a function for each instance to waste memory.

3. Prototype mode: The prototype is a space that each instance can share, we can place properties and methods here, this shows that code reuse solved, the constructor mode wasted memory problem solved. Advantages: The implementation of code reuse, you can judge the category identification, sharing a space does not waste memory. Cons: Due to sharing, there will be problems when setting reference properties, such as person.prototype.address=["Henan", "Hebei", "declaration of two objects Person1 and Person2;person1.address.push (" Beijing " alert (person1.address) → Henan, Hebei, Beijing; alert (person.address) → Henan, Hebei, Beijing. This is a big problem.

4. Combination mode: Combining the advantages of constructors and prototype patterns, attributes are constructed using the prototype settings.

5. Dynamic prototyping, in effect, is to put the method of the prototype declaration in the composition pattern inside the function body, and only load once.

JS Object-Oriented class

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.