481 break;
482
483 case "image":
484 function draw() { 485 pJS.canvas.ctx.drawImage( 486 img_obj, 487 p.x - radius, 488 p.y - radius, 489 radius * 2, 490 (radius * 2) / p.img.ratio, 491 ); 492 } 493
494 if (pJS.tmp.img_type == "svg") {
495 var img_obj = p.img.obj;
Declarations in switch cases are visible to all blocks. It is recommended not to have such declarations.
Writing lexical declarations (let
, const
, function
and class
) within case
/default
clauses is not a good practice.
Such declarations are hoisted into the scope of the entire switch expression, meaning they are visible to all case
clauses in that switch
block.
Though the declaration is visible to all cases, it is only initialized in the clause that it was declared in.
It will be undefined
if used in any other clause.
To ensure that the lexical declaration only applies to the current case clause, wrap your clauses in blocks.
switch (points) {
case 1:
let x = 1;
break;
case 2:
const y = 2;
break;
case 3:
function f() {}
break;
default:
class C {}
}
// Declarations outside switch-statements are valid
const a = 0;
switch (points) {
// The following case clauses are wrapped into blocks using brackets
case 1: {
let x = 1;
break;
}
case 2: {
const y = 2;
break;
}
case 3: {
function f() {}
break;
}
case 4:
// Declarations using var without brackets are valid due to function-scope hoisting
var z = 4;
break;
default: {
class C {}
}
}