1

the javascript code like below:

function aa(){
    console.log(typeof(this))
}

console.log(typeof(aa))

aa()

the output is function and object after running this code.

so why the code output difference type?

thanks in advance!

Courage
  • 543
  • 5
  • 25
  • Because `aa` and `this` refer to different values, one of them not being a function. If you want to learn how `this` works I recommend to read https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch1.md . Also note that `typeof` is not a function but an operator, so you should just be writing `typeof aa`. – Felix Kling Jun 28 '17 at 07:34
  • Take a look here https://stackoverflow.com/questions/1963357/this-inside-function – Alexis Jun 28 '17 at 07:34
  • thanks for the reply, but what about run it in node.js? which is 'this' refer to ? – Courage Jun 28 '17 at 07:49

1 Answers1

3

During the execution of a function that has been called without passing a context this is bound the the global window object of the browser.

this is never the function object itself, unless you pass that explicitly with call or apply.

The output of

function f(){console.log(typeof this);}
f.call(f);

will be function.

6502
  • 112,025
  • 15
  • 165
  • 265
  • thanks, but I run my code with node.js, no window in it. Moreover, I think FUNCTION also a type in javascript? – Courage Jun 28 '17 at 07:40