'set' used outside of binding context.
16 catch (err) { console.error('invalid JSON in data-goatcounter-settings: ' + err) }
17 for (var k in set)
18 if (['no_onload', 'no_events', 'allow_local', 'allow_frame', 'path', 'title', 'referrer', 'event'].indexOf(k) > -1)
19 window.goatcounter[k] = set[k] 20 }
21
22 var enc = encodeURIComponent
'set' used outside of binding context.
14 if (s && s.dataset.goatcounterSettings) {
15 try { var set = JSON.parse(s.dataset.goatcounterSettings) }
16 catch (err) { console.error('invalid JSON in data-goatcounter-settings: ' + err) }
17 for (var k in set) 18 if (['no_onload', 'no_events', 'allow_local', 'allow_frame', 'path', 'title', 'referrer', 'event'].indexOf(k) > -1)
19 window.goatcounter[k] = set[k]
20 }
Description
Variables should be used inside of their binding context.
This helps avoid difficult bugs with variable hoisting.
It is a bad practice to use var
declarations because variables declared using var
can be accessed in a function-wide scope.
They can even be accessed before declaration.
In such cases, their value would be undefined
because only declarations and not initializations are hoisted.
Bad Practice
function doIf() {
if (cond()) {
var build = true;
}
console.log(build);
}
function doIfElse() {
if (cond()) {
var build = true;
} else {
var build = false;
}
console.log(build)
}
Recommended
function doIf() {
let build;
if (cond()) {
build = true;
}
console.log(build);
}