Definition and call of the struct constructor \ method in the js getting started instance \ prototype object _ Javascript tutorial

Source: Internet
Author: User
Javascript: defines and calls the struct constructor \ method of the js getting started instance \ prototype object. Javascript tutorial script language = "javascript">
Function circel (radius)
{// This function defines the class itself. The r below 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.
Circel. PI = 3.14159;
Function area ()
{// Of course, this is the formula for calculating the circular area.
Return this. PI * this. r * this. r;
// Next we assign the function to the circular object of the constructor to make it an instance method,
// 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;
// The following is another function, which compares two parameters and returns a larger one
Function circel_max (a, B)
If (a. r> B. r)
Return B;
// Because the function compares two objects, it is meaningless to regard it as a method operation for a single Circel instance.
// But we do not want to be an independent function, so assign it to a constructor to make it a class method.
Circel. max = circel_max;
// The following code uses various circel Fields
Var c = new circel (1.0); // create an instance of the circel class
C. r = 2.2; // set the 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 the class method circel. max
JScript. js File
/** // * JScript File
The plural is the sum of an imaginary number and an imaginary number. The imaginary number I is the square root of-1.

/** // * The first step of defining a class is to define the constructor of this class. The qualification constructor needs to initialize all Instance functions of the object.
These attributes are the core "state variables". They make each instance of the class different from each other.

Function JScript (real, img)
This. x = real; // real part
This. y = img; // virtual part

/** // * The second step of defining the function class is to define its instance method (or other attributes) in the prototype object of the constructor)
Any property defined by this object will be inherited by all instances of this class.
Note the implicit operation of the instance method on the this keyword. Many methods do not need other parameters.

// Return the size of the complex number, which is defined as the distance from the origin (0, 0) to the complex plane
JScript. prototype. magntasks = function ()
Return Math. sqrt (this. x * this. x + this. y * this. y );
// Returns the opposite number of plural values.
JScript. prototype. negative = function ()
Return new JScript (-this. x,-this. y );
// Replace the JScript object with a string in an effective way, which is a function called when the JScript object is used 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 the JScript object is processed as the original value.
JScript. prototype. valueOf = function () {return this. x ;};

/** // * The third step of defining a class is to define a class method.
Constants and other class variables are used as the properties of the function to construct itself (rather than the properties of the prototype object of the constructor)
Note that static methods do not use the this keyword because they only operate on parameters.
// Calculate the sum of the two plural numbers and return the result
JScript. add () = function (a, B)
Return new JScript (a. x + B. x, a. y + B. y );
// Subtract one complex number from the other and return the result
JScript. suBTract () = function (a, B)
Return new JScript (a. x-B. x, a. y-B. y );
// Calculate the product of two plural numbers and return results
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 pre-defined plural numbers which are defined as class variables so that constants can be used (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

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: 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.