Simple Function Invocation ?

Simple Function Invocation

The single most basic way to invoke a function in JavaScript is to supply the () parenthesis to a function that is just floating around, waiting to be called.

  function doStuff(){
    console.log("I'm doing blah blah!");
    console.log(this);
  }

  doStuff();

This is just a raw function call , except for the logging of “this” to the console.

The Default Value of “this”:

To classify the value of “this” as the default value, when a function is invoked in this manner. The default value is almost always the “global” object for the runtime – but not always.This function call may or may not have a value for “this”. Whether or not it has a value,it all depends on the runtime environment in which the function is being invoked.

Raw Function Calls In A Browser:
The most basic function call, will resolve “this” to the “window” object in a browser. This is the top level object that the JavaScript environment has – the object on which all “global” variables live. If you examine the output of

 console.log(this) 

in your browser, you’ll see everything that the “window” object has as well. In fact, if you compare “this” and “window” in the above function, they will be equal.

  function doStuff(){
    console.log(this === window); //=> true
  }
  doStuff();

For a raw function invocation in a browser, then, the value of “this” will be the “global” or “window” object.

More Info: this

About

My Name is Dileep Singh, Noder, Javascript Lover & NoSql Developer, Fitness Freak, Love to travel new places, learning photography & Music Manic ♯ ♩ ♬

Posted in Javascript Tagged with: