While learning how this
keyword works in JS using VSCode
, I came across a tutorial code
(below).
After executing the code in VSCode the Output I got doesn't match the Output of tutorial or an Online JS editor.
var firstName = "Peter",
lastName = "Ally";
function showFullName () {
console.log (this.firstName + " " + this.lastName);
}
var person = {
firstName :"Penelope",
lastName :"Barrymore",
showFullName:function () {
console.log (this.firstName + " " + this.lastName);
}
}
showFullName (); // ** problem is here **
person.showFullName ();
While executing the program in the latest version of VSCode
:
The output is Undefined Undefined
and Penelope Barrymore
But in the tutorial and Online JS Editor (https://playcode.io/online-javascript-editor) the Output is :
Peter Ally
and Penelope Barrymore
I'm stuck here, Please Explain what is correct and why?
Thanks.