Methods in Constructor Functions
Explore how to define and add methods within JavaScript constructor functions using the 'this' keyword. Understand the difference between methods in object literals and constructors, and how to ensure methods apply to all created objects.
We'll cover the following...
We'll cover the following...
Defining Methods
Methods are defined differently in constructor functions.
Example
We defined our methods inside object literals in the following way:
Now, let’s write the same function but for the constructor function this time:
Explanation
In line 9 of the code for object literal, the method declaration was simple: the name of the function followed by ...