add topic_5
This commit is contained in:
@@ -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>
|
||||
@@ -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);
|
||||
Reference in New Issue
Block a user