0

I am learning js call() function. When I am executing with the let variable (snippet 2), it's showing the desired result, but, when I run the code with the var (snippet 1), it doesn't give the desired results.

var name={
firstname:"amit",
lastname:"shamit"
}

var callName= function(hometown){

console.log(this.firstname+" "+this.lastname+" "+hometown);

}

callName.call(name,"dehradun");
//o/p: undefined undefined dehradun

let name={
firstname:"amit",
lastname:"shamit"
}

let callName= function(hometown){

console.log(this.firstname+" "+this.lastname+" "+hometown);

}

callName.call(name,"dehradun");
//o/p: amit shamit dehradun
Akrit
  • 91
  • 1
  • 4
  • 4
    The first is actually failing based on the name of your var `name` if you change it it will work. see: [Using the variable “name” doesn't work with a JS object](https://stackoverflow.com/questions/10523701/using-the-variable-name-doesnt-work-with-a-js-object) – pilchard Jun 01 '21 at 10:17
  • above comment was helpful. windows.name is a predefined property. – Akrit Jun 01 '21 at 12:11

0 Answers0