Using a Prototyping Object Pattern

An even more advanced method of creating objects is using a prototyping pattern. You implement such 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() ...

Get Learning AngularJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.