Here I made two objects; one has accessor methods created in the constructor, the other in the prototype. Why would one choose one of these over the other?
f
In your first example, you're creating new functions for each instance of the object whenever you instantiate a new one. In the second, only a single copy of the function is created which is used by all the instances.
The second way can save memory. You can also use prototype chaining to implement inheritance.
Btw, your second example won't work as written. the secret variable in spy2 is local to the constructor. In the setSecret and getSecret functions in the prototype, you're accessing a single global variable.