javascript - 'use strict' not stopping hoisting in function scope -
my problem lies here i'm learning javascript not new programming @ all. understand hoisting, strict mode shouldn't produce error , caught either when 6 assigned undeclared variable or document.getelement... assigned x doesn't produce error diagnosis hoisting still going on..which don't , want rid of using strict. using chrome version 42.0.2311.152 m browser
function strictmode(){ 'use strict'; try { x = 6; document.getelementbyid('hoisting').innerhtml = x; var x; } catch(err) { document.getelementbyid('error_report').innerhtml = "there error occured (were in strict mode)" + " " + err.message; } }
variable declarations (i.e. var x;
) valid entire scope written in, if declare after assign. meant "hoisting": var x;
hoisted beginning of scope, , assignment x = 6;
fine because x
has been declared somewhere in scope.
strict mode not change of this. would throw error if omitted var x;
declaration altogether; without strict mode, variable's scope implicitly global scope.
in es2015 (a.k.a. es6), hoisting avoided using let
keyword instead of var
. (the other difference variables declared let
local surrounding block, not entire function.)
Comments
Post a Comment