[removed] How is “function onload() {}” different from “onload = function() {}”?

前端 未结 6 629
慢半拍i
慢半拍i 2021-02-02 11:37

In the answers to this question, we read that function f() {} defines the name locally, while [var] f = function() {} defines it globally. That makes p

6条回答
  •  猫巷女王i
    2021-02-02 12:25

    Many people are correctly pointing out the global / local difference between (UPDATE: Those answers have mostly been removed by their authors now)

    var x = function() {
    

    and

    function x() {
    

    But that doesn't actually answer your specific question as you aren't actually doing the first one of these.

    The difference between the two in your example is:

    // Adds a function to the onload event
    onload = function() {
        alert("hello");
    }
    

    Whereas

    // Declares a new function called "onload"
    function onload() {
        alert("hello");
    }
    

提交回复
热议问题