From 1a822a4169e97558712e180d520f7b66820bf577 Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Mon, 27 Feb 2023 20:14:18 +0700 Subject: [PATCH] add topic_5 --- topic_5/example1/index.html | 10 ++++++++++ topic_5/example1/script.js | 31 ++++++++++++++++++++++++++++ topic_5/example2/index.html | 10 ++++++++++ topic_5/example2/script.js | 40 +++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 topic_5/example1/index.html create mode 100644 topic_5/example1/script.js create mode 100644 topic_5/example2/index.html create mode 100644 topic_5/example2/script.js diff --git a/topic_5/example1/index.html b/topic_5/example1/index.html new file mode 100644 index 0000000..0ce661b --- /dev/null +++ b/topic_5/example1/index.html @@ -0,0 +1,10 @@ + + + + + Animal + + + + + diff --git a/topic_5/example1/script.js b/topic_5/example1/script.js new file mode 100644 index 0000000..81d6e88 --- /dev/null +++ b/topic_5/example1/script.js @@ -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); diff --git a/topic_5/example2/index.html b/topic_5/example2/index.html new file mode 100644 index 0000000..e7725df --- /dev/null +++ b/topic_5/example2/index.html @@ -0,0 +1,10 @@ + + + + + closure + + + + + diff --git a/topic_5/example2/script.js b/topic_5/example2/script.js new file mode 100644 index 0000000..aed0d03 --- /dev/null +++ b/topic_5/example2/script.js @@ -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)