JavaScript - using prototype to create method for specific class

1:  function Dog (breed) {  
2:   this.breed = breed;  
3:  };  
4:  // here we make buddy and teach him how to bark  
5:  var buddy = new Dog("golden Retriever");  
6:  Dog.prototype.bark = function() {  
7:   console.log("Woof");  
8:  };  
9:  buddy.bark();  
10:  // here we make snoopy  
11:  var snoopy = new Dog("Beagle");  
12:  /// this time it works!  
13:  snoopy.bark();  
snoopy can also bark since the bark() method is created for Dog class.

No comments:

Post a Comment