JavaScript Learning Notes 3-constructors, classes, and prototypes

Source: Internet
Author: User

3. Constructors, classes, and prototypes

General statement:

Objects are familiar to us and are often used in programs. So, how do you differentiate between an object and another object? Class to do this, the object is an instance of the class.

The class in JS is not the same as the Java class, but it constructs functions and prototypes to support classes.

3.1 Constructors

When we use objects in a program, we often start with the new object, and then assign the object to the object, and then use it to achieve the function we want. Very often, new a parameter-free object, which we write:

JS Code

var o = {};

In fact, its implementation is the same as the following code:

JS Code

var o = new object();

In the following notation, the object () behind new is a constructor, just as in a program we often customize the constructor, set its prototype, and then new. So what is a constructor?

Constructors are functions that are designed to be used with new. The new object is passed as the value of the This keyword.

What is the function of a constructor? It initializes a newly created object, setting all properties before using the object.

You can define your own constructors (commonly used), and just write a function that adds attributes to this one. Examples of the following:

JS Code

function Rectangle(w,h){
 this.w = w;
 this.h = h;
}
var rec1 = new Rectangle(1,1);//var rec1 = {w:1,h:1}
var rec1 = new Rectangle(2,2);//var rec2 = {w:2,h:2}

Note The above code:

1, the effect of the annotation is the same. So why do we have to define the constructor? Constructors make it easy for you to new multiple objects, and the annotation is written in such a way that you only need to use one object at a time.

2, from the above code can be seen: by defining a function (constructor), in fact, defines a class of objects.

3, the above class of new objects, all ensure that the initialization of the value of W and H. Objects of different W and H values can be obtained depending on the values of W and H of the incoming constructor, but they all have the same properties (such as calculating the area of the same method).

3.2 Prototypes with inheritance

In the example above, if we want to compute the area of the Rec1 object, we need to do this:

JS Code

rec1.area = function(){
 return this.w * this.h
}
var a = rec1.area();

In this case, if the REC2 also has to get its area, define a value for its own square function. In fact, all the rectangle objects have the same method of calculating the area, and we can write this method in the constructor. As follows:

JS Code

function Rectangle(w,h){
 this.w = w;
 this.h = h;
 this.area = function(){this.w*this.h}
}

By this time the new constructor can be used to get the area of Rec1.area ().

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.