MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1// Test function declaration hoisting inside blocks
2// Function declarations should be hoisted to the top of their containing block
3
4console.log("=== Test 1: Function declaration hoisted in regular block ===");
5{
6 foo();
7 function foo() {
8 console.log("foo called - hoisted in block");
9 }
10}
11
12console.log("\n=== Test 2: Function hoisting in arrow function body ===");
13const test2 = () => {
14 bar();
15 function bar() {
16 console.log("bar called - hoisted in arrow function");
17 }
18};
19test2();
20
21console.log("\n=== Test 3: Function hoisting in callback ===");
22const arr = [1];
23arr.forEach((x) => {
24 baz();
25 function baz() {
26 console.log("baz called with", x, "- hoisted in callback");
27 }
28});
29
30console.log("\n=== Test 4: Function hoisting in .map() callback ===");
31const result = [5].map((x) => {
32 return helper(x);
33 function helper(n) {
34 return n * 2;
35 }
36});
37console.log("result should be [10]:", result);
38
39console.log("\n=== Test 5: Nested function calling before definition ===");
40function outer() {
41 inner();
42 function inner() {
43 console.log("inner called - hoisted inside outer");
44 }
45}
46outer();
47
48console.log("\n=== Test 6: Function modifying outer variable before definition ===");
49function test6() {
50 let value = 0;
51 modify();
52 console.log("value after modify should be 42:", value);
53
54 function modify() {
55 value = 42;
56 }
57}
58test6();
59
60console.log("\n=== All block function hoisting tests completed ===");