Create advanced object in JScript

Source: Internet
Author: User

 

Use constructors to create objects
A constructor is a function that calls it to demonstrate and initialize special types of objects. You can use the new keyword to call a constructor. The following is a new example of using constructors.

VaR myobject = new object (); // create a common object without attributes.
VaR mybirthday = new date (1961, 5, 10); // create a date object.
VaR mycar = new car (); // create a user-defined object and initialize its attributes.
The constructor transmits a parameter as the value of the specific this keyword to the newly created null object. Then, the constructor is responsible for initializing the new object (creating attributes and providing their initial values ). After completion, the constructor returns the pair

.

Compile Constructor
You can use the new operator to create and initialize an object with predefined constructors such as object (), date (), and function. Object-Oriented Programming its powerful feature is to define custom constructor to create scripts used in

The ability to customize objects. A user-defined constructor is created to create objects with defined attributes. The following is an example of a user-defined function (note the use of this keyword ).

Function circle (xpoint, ypoint, radius ){
This. x = xpoint; // The X coordinate of the center.
This. Y = ypoint; // y coordinate of the center.
This. r = radius; // the radius of the circle.
}
When the circle constructor is called, the value of the center point and the circle radius are given (all these elements are required to completely define a unique circle object ). At the end, the circle object contains three attributes. The following describes how to use a circle object.

VaR acircle = new circle (5, 11, 99 );
Use a prototype to create an object
When writing constructors, you can use the attributes of the prototype object (which is an attribute of all constructors) to create inheritance attributes and share methods. Prototype attributes and methods are copied to each object in the class by reference, so they all have the same value. Yes

To change the value of the prototype property in an object, the new value will overwrite the default value, but only valid in this instance. Other objects belonging to this class are not affected by this change. The following is an example of using a custom constructor, circle (note this keyword ).

).

Circle. Prototype. Pi = math. Pi;
Function acirclesarea (){
Return this. Pi * This. R * This. R; // the formula for calculating the circular area is? R2.
}
Circle. Prototype. Area = acirclesarea; // The function for calculating the circular area is now a method of the circle prototype object.
VaR A = acircle. Area (); // This is how to call the area function on the circle object.
With this principle, you can define additional attributes for pre-defined Constructors (all with prototype objects. For example, if you want to delete spaces before and after a string (similar to the trim function of VBScript), you can create

Methods.

// Add a function named trim
// A method of the prototype object of the string constructor.
String. Prototype. Trim = function ()
{
// Use a regular expression to separate spaces
// Replace it with a null string.
Return this. Replace (/(^ \ s *) | (\ s * $)/g ,"");
}

// A string with spaces
VaR S = "Leading and trailing spaces ";

// Display "Leading and trailing spaces (35 )"
Window. Alert (S + "(" + S. Length + ")");

// Delete leading and trailing Spaces
S = S. Trim ();
// Display "Leading and trailing spaces (27 )"
Window. Alert (S + "(" + S. Length + ")");

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: info-contact@alibabacloud.com 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.