Source: Internet
Author: User

js| Objects | functions

<script language= "JavaScript" >

function Circel (RADIUS)

{//This function defines the class itself, and the following R is an instance variable defined and initialized by the constructor

This.r=radius;

}

The property is a class variable, and he belongs to a property of the constructor

Circel. pi=3.14159;

function Area ()

{//This is certainly the formula for calculating the circle area.

return this. PI * THIS.R * THIS.R;

}

Here we make it an instance method by assigning the function to the circular object of the constructor.

To be compatible with NAVIGATOR3, we must create and discard an object before the prototype object is generated

New Circel (0);

Circel.prototype.area=area;

Here's another function that compares the two parameters and returns the larger one

function Circel_max (a,b)

{

if (A.R > B.R)

return A;

Else

return b;

}

Because the function compares two objects, it makes no sense to consider him as a single Circel instance method operation.

But we do not want to be a separate function, so we assign him to a constructor so that he becomes a class method

Circel.max=circel_max;

The following code uses the various fields of Circel

var c=new circel (1.0); Create an instance of the Circel class

c.r=2.2; Set a variable for an instance R

var a=c.area (); Call the area method of an instance

var x=math.exp (Circel. PI); Using variable pi in our own calculations

var d=new circel (1.2); Create another Circel instance

var Bigger=circel.max (c,d);//Use class method Circel.max

</script>

Jscript.js file

/**//* JScript Files

The plural is the sum of an imaginary number and an imaginary number, and the imaginary I is the square root of 1.

*/

/**//* The first step in defining a class is to define the constructor of the class, and the eligibility constructor initializes all instance functions of the object

These properties are the core "state variables" that make each instance of the class distinct

*/

function JScript (real,img)

{

this.x=real;//real number of parts

this.y=img;//Virtual Number part

}

/**//* the second step in defining a function is to define his instance method (or other properties) in the stereotype object of the constructor.

Any attribute defined by the object will be inherited by all instances of this class

Note that the instance method implicitly operates on the This keyword, and many methods do not require additional parameters

*/

//Returns the size of the complex number, which is defined as the distance from the origin (0,0) to the complex plane

Jscript.prototype.magnitude=function ()

{

return MATH.SQRT (this.x * this.x + this.y * this.y);

};

//Returns the opposite number of the plural

jscript.prototype.negative=function ()

{

return new JScript (-this.x,- THIS.Y);

};

//To replace a JScript object with a string in an efficient manner, which would be a function called when using a JScript object as a string

Jscript.prototype.tostring=function ()

{

return "{" + this.x + "," + This.y + "}";

};

//Returns the real part of a complex number that is invoked when a JScript object is processed as a raw value

Jscript.prototype.valueof=function () {return this.x;}

The third step of the/**//* definition class is to define the class method.

Constants and other class variables as functions to construct their own properties (rather than the properties of the stereotype objects of constructors)

Note static methods, which do not use the This keyword, because they operate on only the parameters

*/

Calculates and returns the results of two complex numbers

Jscript.add () =function (a,b)

{

return new JScript (a.x + b.x, A.Y + b.y);

};

Reduce another complex number with a complex number and return the result

Jscript.subtract () =function (a,b)

{

return new JScript (a.x-b.x, A.Y-B.Y);

};

Calculates the product of two complex numbers and returns the result

Jscript.multiply () =function (a,b)

{

return new JScript (a.x * b.x-a.y * b.y, a.x * b.x + x.y * b.y);

};

/**//* below are some useful predefined complex numbers that are defined as class variables so that they can be used as constants (note that they are not actually read-only)

*/

Jscript.zero=new JScript (0,0);

Jscript.one=new JScript (1.0);

Jscript.i=new JScript (0.1);

Related Article