To create your own object instance, you must first define a constructor for it. The constructor creates a new object, assigns attributes to the object, and assigns methods when appropriate. For example, the following example defines the constructor for the pasta object. Note This
Keyword, which points to the current object.
// Pasta is a constructor with four parameters.
Function pasta (grain, width, shape, hasegg)
{
// What kind of food is used?
This. Grain = grain;
// How wide? (Value)
This. width = width;
// What is the cross-sectional shape? (String)
This. Shape = shape;
// Add the yolk? (Boolean)
This. hasegg = hasegg;
}
After the object constructor is defined, use the new operator to create an object instance.
VaR spaghetti = new pasta ("wheat", 0.2, "circle", true );
VaR linguine = new pasta ("wheat", 0.3, "Oval", true );
You can add attributes to an object instance to change the instance, but other object definitions generated with the same constructor do not include these attributes, unless you specifically add these attributes, they are not displayed in other instances. If you want
Additional properties are displayed and must be added to the constructor or constructor prototype object (the prototype is discussed in the advanced document.
// Additional attributes of 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
// None are added to the spaghetti object
.
// Add the attribute 'foodgroup' to the pasta prototype object.
//, So that all instances of the pasta object can have this attribute,
// Include the generated instances.
Pasta. Prototype. foodgroup = "carbohydrates"
// Now spaghetti. foodgroup, chowfun. foodgroup, etc.
// The value is carbohydrates ".
Include methods in Definition
You can include methods (functions) in object definitions ). One way is to add an attribute 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.
Called to display the object value.
// Pasta is a constructor with four parameters.
// The first part is the same as above.
Function pasta (grain, width, shape, hasegg)
{
// What food does it use?
This. Grain = grain;
// How wide? (Value)
This. width = width;
// What is the cross-sectional shape? (String)
This. Shape = shape;
// Add the yolk? (Boolean)
This. hasegg = hasegg;
// Add the tostring method (defined as follows ).
// Note that no parentheses are added after the function name;
// This is not a function call,
// Reference the function itself.
This. tostring = pastatostring;
}
// The actual function used to display the content of the past object.
Function pastatostring ()
{
// Return the attributes of the 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 );
// Call tostring () and display the spaghetti object
// Properties (Internet browser required ).
Window. Alert (spaghetti );