In one of the simplest examples, oo JavaScript is described as white

Source: Internet
Author: User
Tags define definition constructor inheritance
Javascript

OO JavaScript is not advanced, trouble on Google out of the Chinese people to introduce the article often RR, and the argument between the different, put together let people see the head big.
Here is a simple example of how to use OO JavaScript to make sense of simplicity.

1. A rather streamlined example

Defining the Circle Class

{
THIS.R = radius;
}
Circle.pi = 3.14159;
Circle.prototype.area = function () {return CIRCLE.PI * THIS.R * THIS.R;}

Using the Circle class


Just understand three keywords:
The first is function, the definition of class in JS world with "function", the content of which is the content of the constructor.

The second is the this pointer, which represents the object that called the function

The third is prototype, which is used to define member functions, to compare specifications and insurance
It can also be written in this way:
function Compute_area () {return CIRCLE.PI * THIS.R * THIS.R;}
Circle.prototype.area=compute_area;

2. Inheritance

Defined

{
This.base=circle;
This.base (RADIUS);
}

Childcircle.prototype=new Circle (0);
function Circle_max (a,b)
{
if (A.R > B.R) return A;

else return B;
}
Childcircle.max = Circle_max;
Use





Alert (Bigger.area ());

Pay attention to two.
1. Define the inheritance Relationship Childcircle.prototype=new Circle (0); 0 of them are used for the placeholder.
2. Invoke the parent class constructor
This.base=circle;
This.base (RADIUS);

3.var-type definition
JS also supports the form of a Var circle={raidus:1.0,pi:3.1415}, as defined by CSS
So circle can also be defined as

var newcircle=
{
r:1.0,
pi:3.1415,
Area:function () {return this. PI * THIS.R * THIS.R;}
};
Alert (Newcircle.area ());
BTW. I'm full and we can look at the OO JavaScript library in Rails--prototype


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.