Method Calls on Objects ?

Method Calls On objects: A basic simple rule is “Method Calls Assign “this” To The Object From Which They Are Called“. JavaScript can be used in an object-oriented manner. In fact, almost everything in JavaScript is an object, whether or not you want it to be. And the most basic and simple form of objects in JavaScript are object literals – the squirrelly bracket syntax with key: value pairs that you see all over the place:

  var myObj = {
    doStuff: function(){
      console.log(this);
    }
  };

  myObj.doStuff();

In this example code, there is an object. That object – an object literal – is assigned to the “myObj” variable. That object also contains a method. A method in object oriented speak, is simply a function that is attached to an object. To invoke this method you only need to call

 "myObj.doStuff();"

and the method will execute.

The question, then, is what does “this” evaluate to in the above method call?

Next …

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