Challenge: Solution Review
Explore how to implement the prototype pattern in JavaScript to optimize memory use by sharing punch and kick methods among ninja instances. Understand why defining functions on the prototype improves efficiency versus each instance holding its own copies. Gain insight into managing object behavior in coding interview challenges.
We'll cover the following...
Solution #
Explanation
In this challenge, you had to create a small game for ninja fighting using the prototype pattern. Two ninjas, ninja1 and ninja2, fight each other in this game. They either have the option to kick or punch. Let’s look at how we achieved this using the prototype pattern.
First, we define the Ninja constructor function. It accepts the name parameter and sets the name property of the current object equal to it. Next, it defines the points property for the instantiated object and sets it equal to 100 ...