What are the functional differences between the following two Javascript prototypes, and are there any benefits for choosing one over the other?
Option 1:
in simple words the difference is in Person.prototype.sayName
all you do is to add a function to the prototype
. just adding new functionality.
In the second Person.prototype = {}
here you are making a new whole object and assign it to prototype
. So you creating new object or overwrite the prototype
with a new object.
First method is good to add many functions as you want on demand. You can add them each one on time, so i think it good when your program is simple and your application objects dose not share much functions or objects among them.
Second method is good if your application objects share some objects(or group of functions as @isaach said in Math functions
) among them.