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

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -