Skip to main content

Posts

Showing posts from October, 2016

Javascript: The prototype pattern

The prototype pattern is composed of two parts, the constructor function, and the prototype. The constructor function has the responsibility of holding the data while the prototype will the define the functions. The advantage of this approach is that all state variables held in the constructor part will be unique to the instance, while all methods defined in the prototype will be shared across all instances. Implementing the constructor part: of the pattern consists in adding any arguments to the function definition and setting in variable using the 'this' keyword. Implementing the prototype part: consists of setting the constructor's prototype to a new literal object containing a property for each function. Finally, it should be noted that by convention, the constructor function should be cased in Pascal case. Typically anytime a function is cased as such in Javascript hints it is a constructor function and should be used with the new keyword.