JavaScript learning 2: Object-oriented and javascript object-oriented

Source: Internet
Author: User

JavaScript learning 2: Object-oriented and javascript object-oriented

Many people know that process-oriented and object-oriented software development ideas, and our JavaScript also has these two development modes. Today, let's take a look at object-oriented things in JavaScript.

Object-oriented languages have a very important feature, that is, the concept of classes. Through classes, we can create any number of objects with the same attributes and methods, javaScript does not have the concept of a class, but it has an object, but this object is not another object.

Create object

<Span style = "font-size: 18px;"> // create an Object var person = new Object (); person. name = 'liany'; person. age = 18; person. run = function () {return this. name + this. age + 'struggling ...... ';}; </Span>

In this way, we create an object and create attributes and methods for the object. this In the run () method represents the person object. This method is the most basic method for JavaScript to create an object. But what should we do if we want to create a similar object like name Lee? You can only re-write the same code again. You can assign the person value to person1 directly. Isn't that easy, but this will overwrite the property value of the person object. What do you do?

To solve this problem, we can use the factory model. Our friends who have been familiar with the design model should know what this is. I will not talk about it here. I will go directly to the Code to see how to implement it:

<Span style = "font-size: 18px;"> // function CreateObject (name, age) {var obj = new Object (); obj. name = name; obj. age = age; obj. run = function () {return this. name + this. age + 'struggling ...... ';}; Return obj ;}; // create an object instance var person1 = CreateObject ('liance', 18); var person2 = CreateObject ('jack', 22 ); alert (typeof person1); // returns Objectalert (person2 instanceof Object); // returns true </span>

But another problem that follows is the identification problem. What does it mean, that is, we cannot figure out the instance of which object they are, because there are two instance objects of the Object type in the memory, you do not know which one is.

What should we do? We have touched on classes in Object-Oriented learning, so we must know the constructor, also called constructor, which is used to initialize classes. We can use constructors to create specific objects, similar to Object objects. For example:

<Span style = "font-size: 18px;"> // constructor mode function Person (name, age) {this. name = name; this. age = age; this. run = function () {return this. name + this. age + 'eat ...... ';};} Var person1 = new Person ('liany', 100); var person2 = new Person ('yang', 100); alert (person1 instanceof Person ); // return true, clearly telling us that person1 belongs to Person </span>

Conclusion: The constructor not only solves the problem of repeated instantiation, but also solves the problem of object recognition. The difference between the constructor and the factory mode is:

1. No explicitly created object (newObject ());

2. directly assign attributes and methods to this object;

3. No return Statement

The only difference between constructors and common functions is that they call different methods. The constructor is also a function and must be called using the new operator. Otherwise, it is a common function.

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.