. A simplified example
You only need to understand three keywords:
The first one is function. The class definition in the JS world uses 'function'. The content in the function is the content of the constructor.
The second is the this pointer, which indicates the object that calls this function.
The third is prototype, which is used to define member functions and compare standards and insurance.
-
- // Define the circle class, which has the member variable R, constant Pi, and calculated area. Area ()
-
- FunctionCircle (RADIUS)
-
- {
-
- This. R = radius;
-
- }
-
-
- Circle. Pi = 3.14159;
-
- Circle. Prototype. Area =Function(){
- ReturnCircle. Pi *This. R *This. R;
-
- }
-
-
- // Use the circle class
-
- VaRC =NewCircle (1.0 );
-
- Alert (C. Area ());
// Define the circle class, which has the member variable R, constant Pi, and the calculated area member function area () <br/> function circle (RADIUS) <br/>{< br/> This. R = radius; <br/>}< br/> circle. pi = 3.14159; <br/> circle. prototype. area = function () {<br/> return circle. pI * This. R * This. r; <br/>}< br/> // use the circle class <br/> var c = new circle (1.0); <br/> alert (C. area ());
In addition, the member function definition can also be written as follows:
- FunctionCompute_area (){
- ReturnCircle. Pi *This. R *This. R;
- }
- Circle. Prototype. Area = compute_area;
Function compute_area () {<br/> return circle. Pi * This. R * This. R; <br/>}< br/> circle. Prototype. Area = compute_area;
2. Inheritance
Note:
1) define the inheritance relationship childcircle. Prototype = new circle (0); where 0 is used for placeholder
2) Call the constructor of the parent class
-
- This. Base = circle;
-
- This. Base (RADIUS );
-
-
- // Define the child class of childcircle
-
- FunctionChildcircle (RADIUS ){
-
- This. Base = circle;
- This. Base (RADIUS );
-
- }
-
-
- Childcircle. Prototype =NewCircle (0 );
-
- FunctionCircle_max (a, B ){
-
- If(A. R> B. R)ReturnA;
-
- Else ReturnB;
-
- }
-
- Childcircle. max = circle_max;
-
-
- // Use the childcircle subclass
-
- VaRC =NewChildcircle (1 );
-
- VaRD =NewChildcircle (2 );
-
- VaRBigger = D. Max (c, d );
-
- Alert (bigger. Area ());
This. base = circle; <br/> This. base (RADIUS); <br/> // defines the childcircle subclass <br/> function childcircle (RADIUS) {<br/> This. base = circle; <br/> This. base (RADIUS); <br/>}< br/> childcircle. prototype = new circle (0); <br/> function circle_max (a, B) {<br/> If (. r> B. r) return a; <br/> else return B; <br/>}< br/> childcircle. max = circle_max; <br/> // use the childcircle subclass <br/> var c = new childcircle (1); <br/> var d = new childcircle (2 ); <br/> var bigger = D. max (c, d); <br/> alert (bigger. area ());
3. var-Type Definition
JS also supports the form of VaR Circle = {raidus: 1.0, Pi: 3.1415}. The syntax is like the definition of CSS.
Therefore, if there is only one circle instance, the following definition method is more concise:
- var newcircle =
- {
- r: 1.0,
- Pi: 3.1415,
- area: function () {
- return This . pI * This . R * This . r;
- }
- };
-
- alert (newcircle. Area ();
VaR newcircle = <br/>{< br/> r: 1.0, <br/> Pi: 3.1415, <br/> area: function () {<br/> return this. pI * This. R * This. r; <br/>}< br/>}; <br/> alert (newcircle. area ());
Let's take a look at the OO JavaScript library-prototype in rails.
In fact, the current Syntax of JavaScript is really not suitable for the so awkward Writing of the OO mode ....