Issues with understanding "this" in javascript -


i have following function. javascript tutorial i'm reading says should output 2, i'm getting undefined in text editor (js fiddle).

what reason? have strict mode vs non-strict mode?

function foo(){     console.log( this.a ); }  var = 2;  foo(); //should output "2" i'm getting undefined. why? 

that's because running code in function wrapper jsfiddle creates. default onload puts code in function runs on load event.

that makes a variable local function, , not global variable. when try access using this.a variable in window.a (as this point window object), variable not global can't found there.

if choose no wrap - in <head> or no wrap - in <body> put code, 2 output.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -