|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<title>Who has the sitting property?</title> |
| 6 | +<script> |
| 7 | +function Dog(name, breed, weight) { |
| 8 | + this.name = name; |
| 9 | + this.breed = breed; |
| 10 | + this.weight = weight; |
| 11 | +} |
| 12 | + |
| 13 | +Dog.prototype.species = "Canine"; |
| 14 | +Dog.prototype.sitting = false; |
| 15 | + |
| 16 | +Dog.prototype.bark = function() { |
| 17 | + if (this.weight > 25) { |
| 18 | + console.log(this.name + " says Woof!"); |
| 19 | + } else { |
| 20 | + console.log(this.name + " says Yip!"); |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +Dog.prototype.run = function() { |
| 25 | + console.log("Run!"); |
| 26 | +}; |
| 27 | +Dog.prototype.wag = function() { |
| 28 | + console.log("Wag!"); |
| 29 | +}; |
| 30 | +Dog.prototype.sit = function() { |
| 31 | + if (this.sitting) { |
| 32 | + console.log(this.name + " is already sitting"); |
| 33 | + } else { |
| 34 | + this.sitting = true; |
| 35 | + console.log(this.name + " is now sitting"); |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +var fido = new Dog("Fido", "Mixed", 38); |
| 40 | +var fluffy = new Dog("Fluffy", "Poodle", 30); |
| 41 | +var spot = new Dog("Spot", "Chihuahua", 10); |
| 42 | +var barnaby = new Dog("Barnaby", "Basset Hound", 55); |
| 43 | + |
| 44 | +spot.bark = function() { |
| 45 | + console.log(this.name + " says WOOF!"); |
| 46 | +}; |
| 47 | + |
| 48 | +fido.bark(); |
| 49 | +fido.run(); |
| 50 | +fido.wag(); |
| 51 | + |
| 52 | +fluffy.bark(); |
| 53 | +fluffy.run(); |
| 54 | +fluffy.wag(); |
| 55 | + |
| 56 | +spot.bark(); |
| 57 | +spot.run(); |
| 58 | +spot.wag(); |
| 59 | + |
| 60 | +barnaby.sit(); |
| 61 | +barnaby.sit(); |
| 62 | +spot.sit(); |
| 63 | +spot.sit(); |
| 64 | + |
| 65 | +console.log("Does spot have a sitting property? " + spot.hasOwnProperty("sitting")); |
| 66 | +console.log("Does fido have a sitting property? " + fido.hasOwnProperty("sitting")); |
| 67 | + |
| 68 | +</script> |
| 69 | +</head> |
| 70 | +<body> |
| 71 | +</body> |
| 72 | +</html> |
| 73 | + |
0 commit comments