Create | object | advanced
Using constructors to create objects
A constructor is a function called to instantiate and initialize a particular type of object . You can invoke a constructor by using the new keyword. A new example of using a constructor is given below.
var myObject = new Object();
Create a generic object with no attributes. var myBirthday = new Date(1961, 5, 10);
creates an
object. var myCar = new Car();
creates a user-defined object and initializes its properties.
Pass a parameter to the newly created empty object by using a constructor as the value of the specific this keyword. The constructor is then responsible for performing an adaptive initialization for the new object (creating the attribute and giving its initial value). When finished, the constructor returns one of the parameters of the object it constructs.
Writing constructors
You can use the new operator to combine predefined constructors such as object (),Date () , and function () to create and initialize an object. The powerful feature of object-oriented programming is the ability to define custom constructors to create custom objects that are used in scripts. A custom constructor is created so that an object with the defined property can be created. The following is an example of a custom function (note the use of this keyword).
function Circle (xPoint, yPoint, radius) { this.x = xPoint;
The coordinates of the center of the Circle
. this.y = yPoint;
the coordinates of the center of the Circle
. this.r = radius;
the radius of the circle. }
When the Circle constructor is invoked, the value of the center point and the radius of the circle are given (all of these elements are necessary to fully define a unique circular object). At the end of the Circle object contains three properties. Here's how to sample the Circle object.
var aCircle = new Circle(5, 11, 99);
Using prototypes to create objects
When writing a constructor, you can create inherited properties and shared methods by using properties of a prototype object that is itself a property of all constructors. The prototype properties and methods are copied to each object in the class by reference, so they all have the same value. You can change the value of a prototype property in one object, and the new value will overwrite the default value, but it is only valid in that 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 the use of this keyword).
Circle.prototype.pi = Math.PI;function ACirclesArea () {
The formula for calculating the circle area is ?r2
.
the function that computes the circle area is now
a method of the object. var a = ACircle.area();
This is how you
call an area function on an object.
With this principle, you can define additional properties for predefined constructors (all with prototype objects). For example, if you want to be able to delete the front and back spaces of a string (similar to the Trim function in VBScript), you can create your own method for the string prototype object.
A method that adds a function named as
a prototype object for the constructor. String.prototype.trim = function()
{
Use a regular expression to replace the front and back spaces
with an empty string. return this.replace(/(^\s*)|(\s*$)/g, "");
}
A string with a space var s = " leading and trailing spaces ";
display " leading and trailing spaces (35)"
window.alert(s + " (" + s.length + ")");
before and after the delete space s = s.trim();
display"leading and trailing spaces (27)"
window.alert(s + " (" + s.length + ")");