JScript to create your own objects

Source: Internet
Author: User
Js|jscript| Create | objects

To create your own objects
To create your own instance of an object, you must first define a constructor for it. The constructor creates a new object, assigns the object properties, and assigns the method at the right time. For example, the following example defines a constructor for the pasta object. Note the use of this keyword, which points to the current object.

The pasta is a constructor with four parameters.
function Pasta (grain, width, shape, Hasegg)
{
What kind of food do you use?
This.grain = grain;

How wide? Numerical
This.width = width;

Cross section shape? String
This.shape = shape;

Do you add egg yolks? (Boolean)
This.hasegg = Hasegg;
}
After you define an object builder, you create an object instance with the new operator.

var spaghetti = new Pasta ("wheat", 0.2, "circle", true);
var linguine = new Pasta ("wheat", 0.3, "oval", true);
You can add properties to an object instance to change the instance, but they are not included in other object definitions that are generated by the same constructor, and are not shown in other instances unless you specifically add those properties. If you want to display additional properties for all instances of an object, you must add them to the constructor or constructor prototype object (the prototype is discussed in an advanced document).

Additional properties of the spaghetti.
Spaghetti.color = "Pale Straw";
Spaghetti.drycook = 7;
Spaghetti.freshcook = 0.5;

var chowfun = new Pasta ("Rice", 3, "flat", false);
Chowfun object or other existing Pasta object
are not added to the Spaghetti object
Three new properties.

  
Add the attribute ' Foodgroup ' to the Pasta prototype object
So that all instances of the pasta object can have this property,
Include those instances that have been generated.
Pasta.prototype.foodgroup = "Carbohydrates"

Now Spaghetti.foodgroup, Chowfun.foodgroup, et cetera.
Contains the value "carbohydrates".
Include methods in the definition
You can include methods (functions) in the definition of an object. One way is to add a property to the constructor that references a function defined elsewhere. For example, the following example expands the pasta constructor defined above to include the ToString method, which is invoked when the value of the object is displayed.

The pasta is a constructor with four parameters.
The first part is the same as above.
function Pasta (grain, width, shape, Hasegg)
{
What kind of food do you use?
This.grain = grain;

How wide? Numerical
This.width = width;

Cross section shape? String
This.shape = shape;

Do you add egg yolks? (Boolean)
This.hasegg = Hasegg;

Add the ToString method here (definition below).
Note that there is no parenthesis after the name of the function;
This is not a function call, but a
A reference to the function itself.
this.tostring = pastatostring;
}

The actual function used to display the contents of the past object.
function pastatostring ()
{
Returns the properties of an object.

Return "grain:" + this.grain + "\ n" +
"Width:" + this.width + "\ n" +
"Shape:" + this.shape + "\ n" +
"Egg:" + Boolean (This.hasegg);
}

var spaghetti = new Pasta ("wheat", 0.2, "circle", true);
Will call ToString () and display the Spaghetti object
Properties (requires an Internet browser).
Window.alert (spaghetti);



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.