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
Post a Comment