JS learning notes-OO question object Creation

Source: Internet
Author: User

Question 1: Introduce the factory to solve repeated code

As mentioned above, it is not difficult to find out how to create an object in JS. It is easy to create an object in basic creation methods, if multiple similar objects are created, a large number of repeated codes are generated.

Solution: factory mode (add a method to create an object, and pass in parameters to avoid duplication)

Function createObject (name, age) {var obj = new Object (); // create Object obj. name = name; obj. age = age; obj. run = function () {return this. name + this. age + 'processing... ';}; return obj; // return object reference };

Question 2: Introduce constructor to solve Object Recognition

Although the above method solves the problem of Avoiding repeated code, it also brings about the problem that the specific Object cannot be identified. The method uses the new Object method internally and finally returns the Object reference, all objects created by calling this method return Object references. Therefore, when the typeof or instanceof operator is used, objects cannot be distinguished.

Solution: Constructor (improved factory method)

Function Box (name, age) {// create object this. name = name; this. age = age; this. run = function () {return this. name + this. age + 'processing... ';};};

Comparison: careful kids shoes should be discovered. The difference between this method and the factory mode in question 1 is that the plaintext execution process of newObject () is omitted, and the return Statement is omitted, these are all automatically executed by the background.

The difference between a constructor and a common function lies in its calling method. It must be called using the new operator or an object impersonating.

Question 3: Introduce prototype attribute objects to solve the Sharing Problem between objects

Each object has a prototype and is also an object. The purpose is to solve the sharing problem. The object created by calling the same constructor will share the attributes and methods in prototype.

Solution: Use the prototype to share data

Function Box () {}// declare a constructor Box. prototype. name = 'lil'; // Add the attribute Box in the prototype. prototype. age = 100; Box. prototype. run = function () {// Add the return this method to the prototype. name + this. age + 'processing... ';};


Comparison:

Create a constructor


Create with prototype <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140524/2014052409104298.jpg" hspace = "50" width = "500" height = "300" alt = "\">

Details: when calling an attribute or method, use the proximity principle to first check whether the instance exists. If not, use isPrototypeOf (), hasOwnPrototy (), test the in operator.

Question 4: Use a combination to share and transmit Parameters

The object created in the prototype mode skips the initialization process of passing parameters in the constructor. This is both a disadvantage and an advantage. The disadvantage is that the object initialization value is the same, in addition, if the prototype property contains a reference type, the corresponding attribute of another object is changed.

Solution: Composite constructor + prototype mode (solving the problem of sharing and passing parameters)

Function Box (name, age) {// use the constructor that is not shared this. name = name; this. age = age; this. family = ['father ', 'Mother', 'sister '];}; Box. prototype = {// share the original constructor: Box, run: function () {return this. name + this. age + this. family ;}};


Details: This method uses constructor and prototype to analyze the object to be created and add the content to be shared to the prototype. If this method is not required, it is placed in the constructor. This is the combination.

Optimization: it is inevitable that the split statement is somewhat weird. We will merge the two parts.

Dynamic Prototype mode (initialize the prototype when the sharing method is called for the first time and will not be initialized later)

Function Box (name, age) {// encapsulate all information in the function body this. name = name; this. age = age; if (typeof this. run! = 'Function') {// only initialize Box in the first call. prototype. run = function () {return this. name + this. age + 'processing... ';};}}


Knot:

In learning JS, we still need to understand the Orthodox object-oriented language. Here we learned how to use constructors and prototypes to create objects and understand the concepts of the two, it will be helpful for in-depth object-oriented learning in later JS. Different creation methods are used to solve different problems and understand these methods for on-demand use.

Related Article

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.