add topic_5

This commit is contained in:
2023-02-27 20:14:18 +07:00
parent 838e315103
commit 1a822a4169
4 changed files with 91 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Animal</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
+31
View File
@@ -0,0 +1,31 @@
function Animal(name, energy) {
let obj = Object.create(Animal.prototype);
obj.name = name;
obj.energy = energy;
return obj;
}
Animal.prototype.eat = function (amount) {
console.log(`${this.name} is eating.`);
this.energy += amount;
}
Animal.prototype.sleep = function (length) {
console.log(`${this.name} is sleeping.`);
this.energy += length;
}
Animal.prototype.play = function (length) {
console.log(`${this.name} is playing.`);
this.energy -= length;
}
const leo = Animal("Leo", 7);
leo.sleep(8);
leo.eat(2);
leo.play(10);
const bob = Animal("Bob", 3);
bob.sleep(8);
bob.eat(2);
bob.play(10);