javascript - variable declared in global scope is not being recognised by function -


the declaration of variable name = michelle in global scope not being recognised function saynameforall(), please let me know issue.

function saynameforall() { console.log(this.name); }   var person1 = { name: "nicholas", sayname: saynameforall };   var person2 = { name: "greg", sayname: saynameforall };   var name = "michael";  person1.sayname();  person2.sayname();  saynameforall(); 

this output of code

it working expected in script, variable can changed in 3 ways:

  • when call inside of object in case object .
  • when use constructor, class function.

  • and when use call(), apply(), or bind(); methods.

other wise global object...

function saynameforall() {  console.log(this.name);  }      var person1 = {  name: "nicholas",  sayname: saynameforall  };      var person2 = {  name: "greg",  sayname: saynameforall  };      var name = "michael";    person1.sayname();   person2.sayname();   saynameforall();


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -