<script language="javascript">
functioncircel(radius)
{//This function defines the class itself. The following r is an instance variable defined and initialized by the constructor.
this.r=radius;
}
//This attribute is a class variable, which is an attribute of the constructor
circle.PI=3.14159;
function area()
{//This is of course the formula for calculating the area of a circle.
return this.PI * this.r * this.r;
}
// Below we make it an instance method by assigning the function to the circular object of the constructor.
//In order to be compatible with Navigator3, we must create and discard an object before the prototype object is generated
newcircel(0);
circle.prototype.area=area;
//The following is another function that compares two parameters and returns the larger one
functioncircel_max(a,b)
{
if(ar > br)
return a;
else
return b;
}
//Since this function compares two objects, it makes no sense to treat it as an operation on a single Circel instance method.
//But we don't want to become an independent function, so we assign it a constructor and make it a class method.
circel.max=circel_max;
//The following code uses various fields of circle
var c=new circel(1.0); //Create an instance of the circel class
cr=2.2; //Set a variable r of the instance
var a=c.area(); //Call the area method of the instance
var x=Math.exp(circel.PI); //Use the 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 file A complex number is the sum of an imaginary number and an imaginary number. The imaginary number i is the square root of -1
*/
/**//*The first step in defining a class is to define the constructor of the class. The qualified constructor initializes all instance functions of the object. These attributes are the core "state variables" that enable each instance of the class to interact with each other. Not the same
*/
function JScript(real,img)
{
this.x=real;//real part
this.y=img;//Imaginary part
}
/**//*The second step in defining a function class is to define its instance method (or other attributes) in the prototype object of the constructor
Any properties defined by this object will be inherited by all instances of this class. Note that instance methods implicitly operate on the this keyword. Many methods do not require other 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);
};
//Return the opposite of a complex number
JScript.prototype.negative=function()
{
return new JScript(-this.x,-this.y);
};
// Convert the JScript object into a string in an efficient way. This will be the function called when using the JScript object as a string.
JScript.prototype.toString=function()
{
return "{"+ this.x +","+ this.y +"}";
};
//Returns the real part of a complex number. This function is called when processing a JScript object as a primitive value.
JScript.prototype.valueOf=function() {return this.x;};
/**//*The third step in defining a class is to define class methods.
Constants and other class variables are properties of the function constructor itself (rather than properties of the constructor's prototype object)
Note that static methods do not use the this keyword because they only operate on parameters
*/
//Calculate the sum of two complex numbers and return the result
JScript.add()=function(a,b)
{
return new JScript(ax + bx ,ay + by);
};
//Subtract one complex number from another complex number and return the result
JScript.subtract()=function(a,b)
{
return new JScript(ax - bx, ay - by);
};
//Calculate the product of two complex numbers and return the result
JScript.multiply()=function(a,b)
{
return new JScript(ax * bx - ay * by , ax * bx + xy * by);
};
/**//*The following are some useful predefined plurals. They 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);