javascript - variable declared in global scope is not being recognised by function -
this question has answer here:
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();
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
Post a Comment