Skip to content

Commit

Permalink
clarifying description and code for static block 'var' hoisting (#35793)
Browse files Browse the repository at this point in the history
  • Loading branch information
getify authored Sep 8, 2024
1 parent 3ab7947 commit 11b7591
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,23 @@ A {{jsxref("Statements/class", "class")}} can have any number of `static {}` ini
These are [evaluated](/en-US/docs/Web/JavaScript/Reference/Classes#evaluation_order), along with any interleaved static field initializers, in the order they are declared.
Any static initialization of a super class is performed first, before that of its sub classes.

The scope of the variables declared inside the static block is local to the block. This includes `var`, `function`, `const`, and `let` declarations. `var` declarations in the block are not hoisted.
The scope of the variables declared inside the static block is local to the block. This includes `var`, `function`, `const`, and `let` declarations. `var` declarations will not be hoisted out of the static block.

```js
var y = "Outer y";

class A {
static field = "Inner y";
static {
// var y only hoisted inside block
console.log(y); // undefined <-- not 'Outer y'

var y = this.field;
}
}

// var defined in static block is not hoisted
// var y defined in static block is not hoisted
// outside the block
console.log(y); // 'Outer y'
```

Expand Down

0 comments on commit 11b7591

Please sign in to comment.