This Keyword

From rbachwiki
Revision as of 18:39, 28 October 2016 by Bacchas (talk | contribs)
Jump to navigation Jump to search

Using this keyword

By using just "name" it will call the global "name" as opposed to the local name

var name = "robert";
var a = {
  name: "John",
  greet: function(){
    console.log('My name is '+ name);
  }
};
a.greet();
-- Output Robert

By using the this.name it will call the variable thats associated with the a.

var name = "robert";
var a = {
  name: "John",
  greet: function(){
    console.log('My name is '+ this.name);
  }
};
a.greet();
-- Output John