Javascript variable scope within onload function -


this question has answer here:

i had similar situation , curious why p not know b is, since p defined within same function b.

var = "a"; window.onload = function() {     var b = "b";     var p = new person();     p.doiknowaorb();  } function person() {     this.name = "nate"; } person.prototype = function(){     var doiknowaorb = function() {         console.log(a);         console.log(b);     };     return {         "doiknowaorb": doiknowaorb     } }(); 

you're accessing b outside function declared in.

the local scope function-oriented.

so in:

window.onload = function() {     var b = "b";     var p = new person();     p.doiknowaorb()' } 

b local variable anonymous (un-named) function connected onload.

but inside function doiknowaorb in p:

person.prototype = function(){     function doiknowaorb() {         console.log(a);         console.log(b);     };     return {         "doiknowaorb": doiknowaorb     } }(); 

there no b. can access a it's global variable.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -