كود PHP:
Script Function Objects
A 'Script' function object is an object with properties and functions created entirely by the script. The objects can have many functions and hold any script objects as properties, both the functions and the properties are defined by the script developer. These objects are most commonly created by Library or Fragment files so that they are self contained and easily reused.
Creating an Instance
To create an instance of a Script function object the keyword "new" is used just as it is for all other objects. The new statement is followed by the name of the type of object wanted. This name refers to a function that is used to construct the Script object. This function is called the 'Constructor' function. The example below shows the creation of a new 'car' Script object:
function car(make, model, year) {
this.make = make;
this.year = year;
this.model = model;
}
myVw = new car("VW", "Golf gti", 1991);
This example creates a new Script function object called myVw. Note that the keyword "this" has been introduced, "this" refers to the current instance of the Script object (in this case the new one we are creating). So the reference 'this.make' means access the property 'make' in the current instance of the Script object defined by the function.
The properties created in the Constructor function are instance properties and are only available in this instance of the Script object. If another instance of the Script object is created then it will have its own version of these properties. These properties can be accessed from outside the function in the same way as all object properties are accessed.
e.g.
make = myVw.make;
The Constructor function that is used to create the Script function object can contain more than just properties, it can also contain references to other functions. These functions then become the functions available as part of the script object. The example below shows the car function extended to include a getCarMake() function.
function car(make, model, year) {
this.make = make;
this.year = year;
this.model = model;
this.getMake = getCarMake;
}
function getCarMake() {
return this.make;
}
myVw = new car("VW", "Golf gti", 1991);
make = myVw.getMake();
The addition of the line 'this.getMake = getCarMake;' in the Constructor function creates a function reference to the getCarMake() function that becomes an object "function". Note that the name of the function reference does not need to be the same as the name of the function.
Shared Properties
Shared properties are properties that are accessible by all instances of the same Script function object within the current page. Shared properties are only available inside the current page and will be lost when the current page finishes execution even if the Script function object is persisted beyond the current page by putting it in the project object.
There is only one instance of a shared properties and when this is changed in one Script object its new value is available in all other instances of the same Script object.
Shared properties are created by the special reference 'prototype'.
e.g.
car.prototype.date = new Date();
myVw.prototype.date = new Date();
this.prototype.date = Date();
The first example creates a shared date property for the 'car' Script object. The second example creates a shared date property for the 'car' Script object using a reference to a previously created instance of the Script object. The third example creates a shared date property from within a Script object function. After any of these examples any other instances of 'car' Script objects created before or after the example will have the shared 'date' property available.