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