How to create an advanced object using JavaScript

Source: Internet
Author: User

Do you know how to create advanced objects in JavaScript? Here, I will describe them through examples. I believe this article will surely help you gain some benefits.

JavaScript creates advanced objects

1) create an object instance using the constructor

Constructors in JAVASCRIPT are the same as other object-oriented languages and cannot be called directly. They are automatically called when an object is created with the new keyword.

The following is a new example of using constructors in JAVASCRIPT.

 
 
  1. VarmyObject = newObject (); // create a common object without attributes.
  2. VarmyBirthday = newDate (1961,5, 10); // create a Date object.
  3. VarmyCar = newCar (); // create a user-defined object and initialize its attributes.

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 the ability to define custom constructor to create custom objects used in scripts.

A user-defined constructor is created to create objects with defined attributes.

The following is an example of a user-defined function. Pay attention to the use of the this keyword ).

 
 
  1. FunctionCircle (xPoint, yPoint, radius ){
  2. This. x = xPoint; // The x coordinate of the center.
  3. This. y = yPoint; // y coordinate of the center.
  4. This. r = radius; // the radius of the circle.
  5. }

When the Circle constructor is called, it is necessary to completely define a unique Circle object to give the value of the center point and the radius of the Circle ).

At the end, the Circle object contains three attributes. The following describes how to use a Circle object.

VaraCircle = newCircle (5, 11, 99 );

2) create an object using a prototype

The prototype keyword can be used in JAVASCRIPT to create member functions and attributes for objects. You can change the value of the prototype attribute in an object. The new value will overwrite the default value, it is valid only for this instance. Other objects belonging to this class are not affected by this change.

The following is an example of using a custom constructor. Use this keyword in Circle ).

 
 
  1. Circle. prototype. pi = Math. PI;
  2. FunctionACirclesArea (){
  3. Returnthis. pi * this. r * this. r; // the formula for calculating the circular area is? R2.
  4. }
  5. Circle. prototype. area = ACirclesArea;
  6. // The function for calculating the circular area is now a method of the CirclePrototype object.
  7. Vara = ACircle. area (); // This is how to call the area function on the Circle object.

The following example shows an instance method defined by the prototype in the String object.

 
 
  1. // Add a function named trim
  2. // A method of the prototype object of the String constructor.
  3. String. prototype. trim = function ()
  4. {
  5. // Use a regular expression to separate spaces
  6. // Replace it with a null string.
  7. Returnthis. replace (/(^ s *) (s * $)/g ,"");
  8. }
  9. // A string with spaces
  10. Vars = "leadingandtrailingspaces ";
  11. // Display "leadingandtrailingspaces (35 )"
  12. Window. alert (s + "(" + s. length + ")");
  13. // Delete leading and trailing Spaces
  14. Ss = s. trim ();
  15. // Display "leadingandtrailingspaces (27 )"
  16. Window. alert (s + "(" + s. length + ")");

3) define static members of a class

Defining static members of a class in JAVASCRIPT can be defined in this way.

For example, if I want to define a static method in the class to return the instance of the class.

You can attach a method object to createNew first:

SplitterBar. createNew = _ createnew;

The following is an example of a method:

 
 
  1. function_cretaenew()  
  2. {  
  3. varbar=newSplitterBar();  
  4. returnbar;  

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