Difference between revisions of "This Keyword"
Jump to navigation
Jump to search
(One intermediate revision by the same user not shown) | |||
Line 13: | Line 13: | ||
</pre> | </pre> | ||
'''By using the this.name it will call the | '''By using the this.name it will call the variable thats associated with the a. ''' | ||
<pre> | <pre> | ||
var name = "robert"; | var name = "robert"; | ||
Line 25: | Line 25: | ||
-- Output John | -- 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