JavaScript Constructor (required knowledge for object learning) _ js object-oriented

Source: Internet
Author: User
With regard to JavaScript constructor, many JavaScript frameworks have emerged, such as jQuery and Ext, which use JavaScript as an object-oriented language for programming, so how does JavaScript implement some object-oriented features? First, let's take a look at how JavaScript defines a constructor. The Code is as follows:


Function A (x)
{
This. x = x;
}
Var obj = new A (5 );
Alert (obj. x );


This code is very simple, but what is important is that we have seen a very surprised result. obj is assigned a property x, just like when we use an instance of a class in C. How is this property generated?

Key statement: this. x = x. This is an attribute declaration and assignment. Here, we will certainly ask, what is this? Why can I use this. x to declare and assign values to attributes?

In fact, this represents the just-instantiated obj, which is equivalent to calling the properties and methods in constructor A using obj.

So how do we define a method in the constructor?

The Code is as follows:


Function A (x, y)
{
This. x = x;
This. y = y;
A. prototype. FunX = function () {alert (x )};
A. prototype. FunY = function () {alert (y )};
}
Var obj = new A (5, 10 );
Alert (obj. x );
Alert (obj. y );
Obj. FunX ();
Obj. FunY ();


The execution result is very simple. The result of 5, 10, 5, and 10 is displayed.

The Code is as follows:


A. prototype. FunX = function () {alert (x )};
A. prototype. FunY = function () {alert (y )};


The Code defines two methods: FunX and FunY. So what if we need to temporarily Add A method to function?

The Code is as follows:


Function A (x, y)
{
This. x = x;
This. y = y;
}
A. prototype. FunX = function () {alert ("5 ")};
Var obj = new A (5, 10 );
Alert (obj. x );
Alert (obj. y );
Obj. FunX ();
A. prototype. FunY = function () {alert ("10 ")};
Obj. FunY ();


After running this code, we can see that the pop-up results are the same as the previous results, but we have defined both methods outside and the FunY method is defined after the instantiation, so what do you see here? Obviously, when we use the obj. FunY () Statement, the code will re-construct the obj and then execute this method. If the code is changed to this way?

The Code is as follows:


Obj. FunY ();
A. prototype. FunY = function () {alert ("10 ")};


Obviously, FunY () won't execute the method.

Next time, I will talk about the JavaScript constructor and prototype. If you have any questions or errors, please correct and discuss them.

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.