Difference between revisions of "This Keyword"
Jump to navigation
Jump to search
Line 10: | Line 10: | ||
}; | }; | ||
a.greet(); | a.greet(); | ||
-- Output Robert | |||
</pre> | </pre> | ||
Line 22: | Line 23: | ||
}; | }; | ||
a.greet(); | a.greet(); | ||
-- Output John | |||
</pre> | </pre> |
Revision as of 17:06, 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 local scope variable
var name = "robert"; var a = { name: "John", greet: function(){ console.log('My name is '+ this.name); } }; a.greet(); -- Output John