JavaScript supports object-oriented development (2)
Source: Internet
Author: User
Do these classes have hierarchical points?
Another good question is whether JavaScript classes have hierarchical points. The answer is definitely there. Let's take a closer look at how it's layered. We can define a rectangle subclass and take figure as the parent class:
function Rectangle (startx, Starty, EndX, EndY) {
This.width = Endx-startx;
This.height = Endy-starty;
This.centerx = (EndX + startx)/2;
This.centery = (EndY + starty)/2;
This.computearea = Computearea;
function Computearea () {
This.area = This.width*this.height;
} }
Rectangle.prototype = new Figure ();
The rectangle object is created with 4 independent variables, and the first four rows are constructors.
The rectangle class contains a method: Computearea (). The last line Rectangle.prototype = new Figure (), and the rectangle class is defined as a subclass inherited from the Figure class.
Let me explain prototype (prototype). Each object constructor has a prototype attribute, which is used to add new properties and methods to all objects. This is why prototypes are used to implement inheritance: Child.prototype = new parent ();. Through the prototype, all the properties and methods of the parent object are added to the child object.
Note that This.centerx,this.centery and area are the properties used in the rectangle class, but they are properties of the figure parent class. Similar to the rectangle class, circle classes can be defined as prototypes of Figure classes. This parent-child relationship can be defined as you need to define the depth; You can create another rectangle subclass. How do I create an instance of a class?
Creating an instance of a class in JavaScript is easy:
Rect = new Rectangle (100,100,900,800);
This creates an object of the rectangle type. The rectangle constructor fills in the values in the attribute width, height, CenterX, and CenterY. The value of the Rect.area property is 0 (0). You can call the area method using this command:
Rect.computearea ();
The value of Rect.area is now 560,000. To invoke the transform method using:
Rect.transform (100,200,0);
The properties of the parent and child objects can be accessed like this:
var ar = Rect.area;
var wi = rect.width;
Can I go beyond the attributes and methods?
As you do in Java, you can transcend attributes and methods. A property or method defined in a subclass can transcend the properties and methods of a parent class with the same name.
Interacting with global variables
JavaScript also supports the use of global variables. Test the scope of the G_area variable in the following code snippet:
<HTML>
<SCRIPT>
var g_area = 20;
function Figure () {
...
This.area=g_area;
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.