An even more advanced method of creating objects is using a prototyping pattern. You implement a pattern by defining the functions inside the prototype attribute of the object instead of inside the object itself. With prototyping, the functions defined in the prototype are created only once, when the JavaScript is loaded, instead of each time a new object is created.
The following example shows the prototyping syntax:
function UserP(first, last){
this.first = first;
this.last = last;
}
UserP.prototype = {
getFullName: function(){
return this.first + " " + this.last;
}
};
Notice that you define the object UserP
and then set UserP.prototype
to include the getFullName()
function. You can include as many functions in the prototype as you would like. Each time a new object is created, those functions will be available.