Javascript constructors and Methods

Source: Internet
Author: User

In this log, the constructor is mentioned in the method of defining functions in javascript. Now, we will introduce the following concepts of the constructor and method of javascript.

These are two useful concepts. I will introduce the object-oriented mechanism of javascript and Its Class Methods later. The epiphany of inheritance has the advantages of being ambiguous and unknown,

Take a closer look.

Constructor:

In javascript, you can create and initialize a new js Object by using the new operator or predefined Constructors (such as Object (), Date (), and Function.

Constructor has the following two features:

It is called by the new operator

What is passed to it is a reference to the newly created null object. this reference is used as the value of the keyword "this", and it also needs to perform proper initialization for the newly created object.

// Define the constructor,
Function User (name, age)
{
This. name = name;
This. age = age;
}

// Instantiate two objects
Var simaopig = new User ('simaopig ', '25 ');
Var xiaoxiaozi = new User ('xiaoxiaozi', '25 ');

Here we need to understand and force ourselves to accept the following idea: the constructor class initializes a specific object, but it does not return this object.

In the above example, we can see that the constructor references this. It only initializes the object passed in by this and does not return anything.

Method:

The so-called method is actually a javascript function called through an object.

What is actually a function? What are the differences between functions and methods? Does this seem to be a philosophical concept? What is chicken? That's the egg ..

A function is actually a numeric value. It has no essential difference with a method. The only difference between two names is to emphasize the role of an object.

Because you can assign a function to any variable, including the attributes of the object.

// Instantiate object o
Var o = new Object ();

// Assign the function fun to the attribute method OF o
O. method = fun;

// Declare the function fun
Function fun ()
{
Return '20140901 ';
}

// Call the properties of o. The result is displayed by the students. The value is 250.

Alert (o. method ());

A method has a very important attribute, that is, inside the method body, the value of this keyword is the object that calls this method.

Any function used as a method will get an additional actual parameter, that is, the object that calls the function method, that is, this I just told you.

Why do I say that a function is actually a method, and a method is a function?

A function is a value stored in a variable, and that variable is just an attribute of a Global Object. Therefore, when you call a function, you are actually calling a method of a global object.

However, the real difference between a function and a method lies in the design and purpose. A method is used to operate on this object, and a function is usually independent and does not need this object.

I don't want to talk about anything here. However, if I don't copy this example, it's inevitable that people who really need it will be dizzy, especially when I look at my next log ..

Therefore, copy.

// Defines the rectangle of the constructor, which has two attributes: width and height.

Function Rectangle (w, h)
{
This. width = w;

This. height = h;
}

// Define a method to calculate the area. this is used here.

Function compute_area ()
{
Return this. width * this. height;
}

// Strength object rect

Var rect = new Rectangle (3, 4 );

// Use the compute_area function as an attribute of the rect object by assigning a value to the property.

Rect. area = compute_area;

// Call the new method to obtain the area result as 3*4.

Var are = rect. area ();

The above code has a problem, that is, before I call the rect object method every time, I have to assign the method an attribute to it.

Then the newly instantiated object, such as rect2, needs to be retrieved in this way. Trouble.

What if I declare it directly in the constructor? Can I solve the problem by declaring a method for all the objects used for instantiation?

NO, although you are smart, because

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.