Different between a protoype vs adding a method in the constructor


Please see -----A and -----B, are they the same? If they are, what is the purpose of proto-type?

function Dog (breed) {
this.breed = breed;
this.bark = function() { // the same as protoype ----- A
// here we make buddy and teach him how to bark
var buddy = new Dog("golden Retriever");
Dog.prototype.bark = function() { //same as the method added in the "Dog" constructor?? ----- B

// here we make snoopy
var snoopy = new Dog("Beagle");
/// this time it works!


proto type extends an object, so you can add methods later, if, for whatever reason you can't add it directly to the constructor.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.