Simple analysis of javascript object-oriented and prototype _ jquery

Source: Internet
Author: User
To demonstrate that JavaScript is a thorough object-oriented language, it is necessary to start with the object-oriented concept and discuss several concepts of object-oriented: 1. everything is an object. objects are encapsulated and inherited. message communication is used between objects, and information hiding exists. in this article, refer to the section from JavaScript advanced programming, object-oriented and prototype:

1. factory model

ECMAScript can create objects in Factory mode:

// Factory mode function createObject (name, age) {var obj = new Object (); // create Object obj. name = name; // add the property obj. age = age; obj. run = function () {// add method return this. name + this. age + 'running... ';}; return obj; // return object reference}; var obj1 = createObject ('Lil', 100 ); // create the first object var obj2 = createObject ('Jack', 200); // create the second object // alert (obj1.run ()); // Print the run () method of the first object instance // alert (obj2.run (); // Print the run () method of the second object instance () method // alert (typeof obj1); // alert (typeof obj2); alert (obj1 instanceof Object); // truealert (obj2 instanceof Object); // true

Objects created in the factory mode solve the problem of repeated instantiation, but the problem of Object recognition cannot be solved (all objects are objects). therefore, to solve the problem of Object recognition, we use the following constructor.

2. constructor

// Create a function Person (name, age) using the constructor {// all constructors are Object this. name = name; this. age = age; this. run = function () {return this. name + this. age + "ing... ";};}; var person1 = new Person ('zhu1', 100); var person2 = new Person ('zhu2', 200); alert (person1.run ()); alert (person2.run (); alert (person1 instanceof Object); // turealert (typeof person2); // Personalert (person2 instanceof Person ); // truevar person3 = new Object (); Person. call (person3, 'zhu3', 300); // Object impersonating. person3 is of the Object type and impersonate the Person type alert (person3.run ());

In the constructor, this indicates the reference of the current scope object. if the global scope of this indicates the window object, if it is in the constructor body, it indicates the object declared by the current constructor.

The constructor method solves the problem of repeated instantiation and solves the problem of object recognition. the differences between the constructor method and the factory method are as follows:

1. create Object (new Object () not displayed in the constructor method ());

2. directly assign the attribute and method value to this;

3. no return statement;

4. However, the new operator must be used to create constructors;

The above is all the content of this article. I hope you will like it.

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.