Helen 3 tahun lalu
induk
melakukan
9730ab3d6e
2 mengubah file dengan 92 tambahan dan 0 penghapusan
  1. 12 0
      Closures/index.html
  2. 80 0
      Closures/js/script.js

+ 12 - 0
Closures/index.html

@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+<body>
+    <script src="js/script.js"></script>
+</body>
+</html>

+ 80 - 0
Closures/js/script.js

@@ -0,0 +1,80 @@
+function taskMakeProfileTimer() {
+    function makeProfileTimer() {
+        let startTime = performance.now();
+
+        function calc() {
+            let endTime = performance.now();
+            let result = endTime - startTime;
+            return result
+        }
+        return calc;
+    }
+
+    let timer = makeProfileTimer()
+    alert('Время выполнения');
+    alert(timer());
+}
+// taskMakeProfileTimer()
+
+
+function taskMakeSaver() {
+    function makeSaver(fn) {
+        let result;
+        let doFunc = true;
+
+        return function () {
+            if (doFunc) {
+                result = fn();
+                doFunc = false;
+            }
+            return result;
+        }
+    }
+    let nameSaver = makeSaver(() => prompt('Введите свое имя!'))
+    alert('Имя еще не спрашивали!')
+    let name1 = nameSaver()
+    alert('Только что спросили имя!')
+    let name2 = nameSaver()
+    alert("Второй раз спрашиваете?")
+    alert("Привет , " + nameSaver() + (nameSaver() === nameSaver() ? ". Готово!" : 'Какой-то бред!'))
+}
+// taskMakeSaver()
+
+
+function finalCountdown(i) {
+    return setTimeout(function () {
+        if (i === 0) {
+            console.log('Поехали!')
+        } else {
+            console.log(i)
+            return finalCountdown(--i)
+        }
+    }, 1000)
+}
+// finalCountdown(5)
+
+
+function myBind(fn, context, arr) {
+    return (...params) => {
+        let indexParam = 0;
+        let newArray = arr.map((i) => (i === undefined ? params[indexParam++] : i))
+        return fn.call(context, ...newArray)
+    }
+}
+
+
+let pow5 = myBind(Math.pow, Math, [undefined, 5])
+let cube = myBind(Math.pow, Math, [undefined, 3])
+pow5(2) // => 32, 
+cube(3) // => 27
+
+
+let chessMin = myBind(Math.min, Math, [undefined, 4, undefined, 5, undefined, 8, undefined, 9])
+chessMin(-1, -5, 3, 15) // вызывает Math.min(-1, 4, -5, 5, 3, 8, 15, 9), результат -5
+
+
+
+let zeroPrompt = myBind(prompt, window, [undefined, "0"])
+let someNumber = zeroPrompt("Введите число")
+
+myBind((...params) => params.join(''), null, [undefined, 'b', undefined, undefined, 'e', 'f'])('a', 'c', 'd') === 'abcdef'