Difference between “anonymous function” and “function literal” in JavaScript?

后端 未结 5 1341
青春惊慌失措
青春惊慌失措 2020-12-09 12:55

The book Learning JavaScript defines anonymous functions as follows...

Functions are objects. As such, you can create them - just like a String

5条回答
  •  我在风中等你
    2020-12-09 13:28

    This:

    new Function("toWhom", "alert('Hi' + toWhom);")
    

    and this:

    function(toWhom) { alert('Hi' + toWhom); }
    

    are two expressions that produce the same result - they return a new anonymous function object.

    The second expression (and only the second expression) is called a function expression. You may also call it a function literal (although we could argue that a function declaration is also a function literal).

提交回复
热议问题