Difference between revisions of "This Keyword"

From rbachwiki
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 10: Line 10:
};
};
a.greet();
a.greet();
-- Output Robert
</pre>
</pre>


'''By using the this.name it will call the local scope variable'''
'''By using the this.name it will call the variable thats associated with the a. '''
<pre>
<pre>
var name = "robert";
var name = "robert";
Line 22: Line 23:
};
};
a.greet();
a.greet();
-- Output John
</pre>
</pre>
==[[#top|Back To Top]]-[[Main_Page| Home]] - [[Java Script|Category]]==

Latest revision as of 18:51, 28 October 2016

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

Back To Top- Home - Category