Last active
November 10, 2018 18:02
-
-
Save corocoto/5420ceaeffdee9b1e255bedfce36f57b to your computer and use it in GitHub Desktop.
Наследование в цепочке прототипов
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Dog(name, breed, weight) { | |
| this.name = name; | |
| this.breed = breed; | |
| this.weight = weight; | |
| } | |
| Dog.prototype.species = "Canine"; | |
| Dog.prototype.bark = function () { | |
| if (this.weight > 25) { | |
| console.log(this.name + " says Woof!"); | |
| } else { | |
| console.log(this.name + " says Yip!"); | |
| } | |
| }; | |
| Dog.prototype.run = function () { | |
| console.log("Run!"); | |
| }; | |
| Dog.prototype.wag = function () { | |
| console.log("Wag!"); | |
| }; | |
| Dog.prototype.sitting = false; | |
| Dog.prototype.sit = function (){ | |
| if (this.sitting) { | |
| console.log(this.name + " is already sitting"); | |
| } else { | |
| this.sitting=true; | |
| console.log(this.name + " is now sitting"); | |
| } | |
| }; | |
| function showDog(name, breed, weight, handler) { | |
| Dog.call(this, name, breed, weight); | |
| this.handler = handler; | |
| } | |
| showDog.prototype = new Dog(); | |
| //задаем конструктор ShowDog | |
| showDog.prototype.constructor = showDog; | |
| showDog.prototype.league="Webville"; | |
| showDog.prototype.stack = function () { | |
| console.log("Stack"); | |
| }; | |
| showDog.prototype.bait = function() { | |
| console.log("Bait"); | |
| }; | |
| showDog.prototype.gait = function(kind) { | |
| console.log(kind + "ing"); | |
| }; | |
| showDog.prototype.groom = function() { | |
| console.log("Groom"); | |
| }; | |
| var fido = new Dog("Fido", "Mixed", 38); | |
| var fluffy = new Dog("Fluffy", "Poodle", 30); | |
| var spot = new Dog("Spot", "Chihuahua", 10); | |
| var scotty = new showDog("Scotty", "Scottish Terrier", 15, "Cookie"); | |
| var beatrice = new showDog("Beatrice", "Pomeranian", 5, "Hamilton"); | |
| fido.bark(); | |
| fluffy.bark(); | |
| spot.bark(); | |
| scotty.bark(); | |
| beatrice.bark(); | |
| scotty.gait("Walk"); | |
| beatrice.groom(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment