Functions Javascript

[Q.1] - (function() { return (() => this.x).bind({ x: ‘inner’ })(), (() => this.x)() }).call({ x: ‘outer’ });

I don’t understand this question and how it’s working. please explain.


[Q.2] - function constfuncs() { var funcs = []; for(var i = 0; i < 10; i++) funcs[i] = function() { return i; }; return funcs; } var funcs = constfuncs(); funcs5 What does the last statement return ?

It should return 4 because there in the array of “funcs” at index of 5 there will be 4.


[Q.3] - function f(){ return f; } new f() instanceof f; What is the output?

Please explain.

Hi @theonlyritik_3b4a4a5eb9f6c783,

  • Ans 1 : To understand the bind function please visit link . The value to be passed as the this parameter to the target function func when the bound function is called. If the function is not in strict mode, null and undefined will be replaced with the global object, and primitive values will be converted to objects. The value is ignored if the bound function is constructed using the new operator.

  • Ans 2 : Did’nt really get your question please be clear with the function code and the question you are trying to ask.

  • Ans 3 : This will return false as we are checking if new instance of f is the instance of the existing function f hence false is returned.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Hi Malya, thanks for the replies. Everytime you mentioned link. I first tried myself to solve questions by googling or anyhow and then if I don’t understand, I ask. But your answers is like I am reading it from google again. Can you make it more clear please so that as fresher I can understand better. Also for the [Q.2] it’s exact question I had copied and pasted from the [WD.2] Quiz on Functions.

Hi @theonlyritik_3b4a4a5eb9f6c783,
Ans 1 :
(function () { return (() => this.x).bind({ x: "inner" })(), (() => this.x)(); }.call({ x: "outer" }));

The answer is outer due to the bind function used to bind the context of this.

Ans 2 :

function constfuncs() { var funcs = []; for (var i = 0; i < 10; i++) funcs[i] = function () { return i; }; return funcs; } var funcs = constfuncs(); funcs[5]();

The output for this is 10, because max limit for i is 10 , in the array funcs we are storing the function and not the value of the number hence upon calling the function at any index say funcs5 it returns 10 because we are storing the refrence of the functiom in funcs[i] and the in the last statement we are calling that function till that time the value of i has reached its max (10 in the present case) hence it returns 10 and not 4. we can discuss this in chat if any further doubts.

Ans 3 :

function f() { return f; } new f() instanceof f;

This will return as false as this checks if the function f is an instance of f(the returned object).

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.