questions 2 Flashcards

1
Q

Could you explain ““this”” in js?
What will ““this”” be equal?
And if we are in a nodeJS environment?

A

the global object in NodeJs will be .process instead of .window

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to redefine ‘this’?

A

call, apply, bind

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a function in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a closure?

A

my answer + JS tries to find variable in local scope and if not then it tries to find var inside upper scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
for(var i = 0; i < 10; i++) { setTimeout(function() {
console.log(i); }, 1000);
}
Could you explain this example? Could you fix this one?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
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();
A

5 because setTimeout function here has a global object .window as a context

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
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]();
}
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write function sum console.log(sum(1)(2)(3)); // 6

A

function a(x) {return function(y){return function(z){return x + y + z}}}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does js engine understand that this is a function?

A

because we call them in the moment when we call them

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Please, write the function that receives several arrays and finds the maximum value in all of them.

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly