questions 2 Flashcards
Could you explain ““this”” in js?
What will ““this”” be equal?
And if we are in a nodeJS environment?
the global object in NodeJs will be .process instead of .window
How to redefine ‘this’?
call, apply, bind
What is a function in JavaScript?
All functions in JavaScript are objects. They are the instances of the Function type. Since functions are objects, they have properties and methods like other objects.
What is a closure?
my answer + JS tries to find variable in local scope and if not then it tries to find var inside upper scope
for(var i = 0; i < 10; i++) { setTimeout(function() { console.log(i); }, 1000); } Could you explain this example? Could you fix this one?
use let, because of how var works inside the block scope(it resigning every iteration). when we use a let keyword our scope is running correctly inside of it.
var x = 5; var obj = { x: 10, doIt: function doIt(){ var x = 20; setTimeout(function(){ console.log(this.x); }, 10); } }; function time() { console.log(this.x) } obj.doIt();
5 because setTimeout function here has a global object .window as a context
var funcs = []; for (var i = 0; i < 3; i++) { funcs[i] = function() { console.log("My value: " + i); }; }
for (var j = 0; j < 3; j++) { funcs[j](); }
3 times, returns 3 because of var keyword and how it works inside the block scope(it resigning every iteration) if we use let it will return us the correct values.
Write function sum console.log(sum(1)(2)(3)); // 6
function a(x) {return function(y){return function(z){return x + y + z}}}
How does js engine understand that this is a function?
because we call them in the moment when we call them
Please, write the function that receives several arrays and finds the maximum value in all of them.
function a (...arrays) { -> rest syntax let max = -Infinity; arrays.forEach((arr) => { const arrMax = Math.max(...arr); -> spred syntax if(max < arrMax) { max = arrMax; } }); return max; } undefined a([1, 35, 67], [5, 99, 45], [120]); 120 'function b(...arrays) -> arrays = [arr1, arr2, arr3, arrn]' "function b(...arrays) -> arrays = [arr1, arr2, arr3, arrn]" `arr - [1,2,3,4, n], Math.max(...arr) -> Math.max(1,2,3,4, n)` "arr - [1,2,3,4, n], Math.max(...arr) -> Math.max(1,2,3,4, n)" Infinity Infinity Infinity > 99999999999999999999999 true -Infinity < -99999999999999999999999 true