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)