|
@@ -0,0 +1,92 @@
|
|
|
+/* makeProfileTimer */
|
|
|
+
|
|
|
+function makeProfileTimer() {
|
|
|
+ let time1 = performance.now()
|
|
|
+ return function t() {
|
|
|
+ let time2 = performance.now()
|
|
|
+ return time2 - time1
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+var timer = makeProfileTimer()
|
|
|
+alert('Замеряем время работы этого alert');
|
|
|
+alert(timer());
|
|
|
+
|
|
|
+/* makeSaver */
|
|
|
+
|
|
|
+function makeSaver(func){
|
|
|
+ let result = func();
|
|
|
+
|
|
|
+ return function(){
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+var saver = makeSaver(Math.random)
|
|
|
+ var value1 = saver()
|
|
|
+ var value2 = saver()
|
|
|
+ value1 === value2
|
|
|
+
|
|
|
+ var saver2 = makeSaver(() => console.log('saved function called') || [null, undefined, false, '', 0, Math.random()][Math.ceil(Math.random()*6)])
|
|
|
+ var value3 = saver2()
|
|
|
+ var value4 = saver2()
|
|
|
+
|
|
|
+ value3 === value4
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ let namePrompt = prompt.bind(window, 'Как тебя зовут?')
|
|
|
+ let nameSaver = makeSaver(namePrompt)
|
|
|
+ alert(`Привет! Prompt еще не было!`)
|
|
|
+ alert(`Привет ${nameSaver()}. Только что запустился prompt, первый и последний раз`)
|
|
|
+ alert(`Слушай, ${nameSaver()}, го пить пиво. Ведь prompt был только один раз`)
|
|
|
+
|
|
|
+/* Final Countdown */
|
|
|
+
|
|
|
+(function (time = 0) {
|
|
|
+ for (let i = 5; i >= 0; i--) {
|
|
|
+ setTimeout(() => {
|
|
|
+ if (i) {
|
|
|
+ console.log(i)
|
|
|
+ } else {
|
|
|
+ console.log("поехали")
|
|
|
+ }
|
|
|
+ }, (time++) * 1000)
|
|
|
+ }
|
|
|
+ })()
|
|
|
+
|
|
|
+/* MyBind */
|
|
|
+
|
|
|
+var myBind = function(fn, context, arr) {
|
|
|
+
|
|
|
+ return function() {
|
|
|
+ let index = 0
|
|
|
+ var fnArgs = [].slice.call(arguments);
|
|
|
+
|
|
|
+ return fn.apply(context, arr.map(i => {
|
|
|
+
|
|
|
+ if(i === undefined){
|
|
|
+ let newValue = fnArgs[index]
|
|
|
+ index++
|
|
|
+ return newValue
|
|
|
+ }
|
|
|
+ else return i
|
|
|
+ }));
|
|
|
+ };
|
|
|
+ };
|
|
|
+
|
|
|
+ var pow5 = myBind(Math.pow, Math, [undefined, 5])
|
|
|
+ console.log(pow5(2))
|
|
|
+
|
|
|
+ var cube = myBind(Math.pow, Math, [undefined, 3])
|
|
|
+ console.log(cube(3))
|
|
|
+
|
|
|
+ var chessMin = myBind(Math.min, Math, [undefined, 4, undefined, 5,undefined, 8,undefined, 9])
|
|
|
+ console.log(chessMin(-1,-5,3,15))
|
|
|
+
|
|
|
+ var zeroPrompt = myBind(prompt, window, [undefined, "0"])
|
|
|
+ var someNumber = zeroPrompt("Введите число")
|
|
|
+
|
|
|
+ console.log(
|
|
|
+ myBind((...params) => params.join(''), null, [undefined, 'b', undefined, undefined, 'e', 'f'])('a','c','d') === 'abcdef'
|
|
|
+ )
|