0

In this example, I am passing foo.bar to an IIFE, which returns executing 1st-argument. I am passing a function which gets executed, returning this (pointing to window).

So, the return is stored in a which should be window. Then, why it is returning me the argument itself?

var foo = {
  bar: function() { 
    console.log("***", this);
    return this; 
  },
  baz: 1
};

var a = (function(){
  console.log("window?", this===window);
  return arguments[0](); 
})(foo.bar);

console.log("a", a);

16
  • Is there a real world use for this because I wouldn't write any code like this at all. You've just found the most confusing possible way to call a function. Also, is this strict mode or sloppy mode? Commented May 29, 2021 at 2:43
  • @jfriend00 - asked in some interview, and I am confused about the answer. Just wanna know the mechanism, why not getting window as answer. Commented May 29, 2021 at 2:44
  • "this (pointing to window)" - what makes you assume that? Please share your line of thought. Commented May 29, 2021 at 2:47
  • Yes, but still dont get, why getting wrong answer as output! :-/ Commented May 29, 2021 at 2:51
  • 1
    Note: I you change return arguments[0]() to let x = arguments[0]; let y = x(); return y;, you do get window. Commented May 29, 2021 at 2:59

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.