1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:17:45 +00:00

LibJS: Parse FunctionExpressions

FunctionExpression is mostly like FunctionDeclaration, except the name
is optional. Share the parsing logic in parse_function_node<NodeType>.

This allows us to do nice things like:

    document.addEventListener("DOMContentLoaded", function() {
        alert("Hello friends!");
    });
This commit is contained in:
Andreas Kling 2020-03-19 11:52:56 +01:00
parent b1b4c9844e
commit 07679e347c
3 changed files with 21 additions and 5 deletions

View file

@ -154,6 +154,8 @@ class FunctionDeclaration final
: public Statement
, public FunctionNode {
public:
static bool must_have_name() { return true; }
FunctionDeclaration(String name, NonnullRefPtr<ScopeNode> body, Vector<String> parameters = {})
: FunctionNode(move(name), move(body), move(parameters))
{
@ -169,6 +171,8 @@ private:
class FunctionExpression final : public Expression
, public FunctionNode {
public:
static bool must_have_name() { return false; }
FunctionExpression(String name, NonnullRefPtr<ScopeNode> body, Vector<String> parameters = {})
: FunctionNode(move(name), move(body), move(parameters))
{