jsfn:expanding_functionality_through_prototype
Ajout de méthodes via l'objet prototype de l'objet (un “sous-objet” donc)
- prototypal inheritance
- signifie que l'on peut baser les fonctionnalités d'un objet sur un autre objet
- Chaque objet peut être basé sur un autre (et par matter of fact, chaque objet l'est)
- prototype object gives you access
var speak = function(saywhat) { console.log(saywhat); } var Dog = function() { var name, breed; } var Cat = function() { var name, breed; } Dog.prototype.speak = speak; Cat.prototype.speak = speak; firstDog = new Dog; firstdog.name = "José"; firstdog.breed = "chienchien"; firstdog.speak("woof"); firstDog = new Cat; firstdog.name = "Louis-Philippe"; firstdog.breed = "chat"; firstdog.speak("BOUFFER");
- plusieurs objets peuvent hériter
- tous les objets héritent de propriétés
- Declarations unherit from Function
- Function constructor inherit from Object
jsfn/expanding_functionality_through_prototype.txt · Last modified: 2016/01/27 23:30 by leo