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

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>

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);

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>closure</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

View File

@ -0,0 +1,40 @@
function a7(b) {
return function () {
return b -= 7;
}
}
const a = a7(100)
console.log(a())
console.log(a())
console.log("hello")
function printMap(obj, func) {
return function (...args) {
func.apply(obj, args);
}
}
person = {name: "Вова"}
function logPerson() {
console.log(`${this.name}`)
}
const f = printMap(person, logPerson)
f()
function printNumbers(from, to) {
let timerId = setInterval(function() {
if (from >= to) {
console.log(from)
from -= 1
}
if (from < to) {
clearTimeout(timerId);
}
}, 1000);
}
printNumbers(10, 5)