mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-31 21:29:06 +00:00
LibJS: Hoist function declarations
This patch adds function declaration hoisting. The mechanism is similar to var hoisting. Hoisted function declarations are to be put before the hoisted var declarations, hence they have to be treated separately.
This commit is contained in:
parent
e162b59a5e
commit
2579d0bf55
Notes:
sideshowbarker
2024-07-19 05:48:41 +09:00
Author: https://github.com/nooga
Commit: 2579d0bf55
Pull-request: https://github.com/SerenityOS/serenity/pull/2497
6 changed files with 73 additions and 15 deletions
37
Libraries/LibJS/Tests/function-hoisting.js
Normal file
37
Libraries/LibJS/Tests/function-hoisting.js
Normal file
|
@ -0,0 +1,37 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var callHoisted = hoisted();
|
||||
function hoisted() {
|
||||
return true;
|
||||
}
|
||||
assert(hoisted() === true);
|
||||
assert(callHoisted === true);
|
||||
|
||||
{
|
||||
var callScopedHoisted = scopedHoisted();
|
||||
function scopedHoisted() {
|
||||
return "foo";
|
||||
}
|
||||
assert(scopedHoisted() === "foo");
|
||||
assert(callScopedHoisted === "foo");
|
||||
}
|
||||
assert(scopedHoisted() === "foo");
|
||||
assert(callScopedHoisted === "foo");
|
||||
|
||||
const test = () => {
|
||||
var iife = (function () {
|
||||
return declaredLater();
|
||||
})();
|
||||
function declaredLater() {
|
||||
return "yay";
|
||||
}
|
||||
return iife;
|
||||
};
|
||||
assert(typeof declaredLater === "undefined");
|
||||
assert(test() === "yay");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue