[removed] what's the difference between a function name & function reference?

前端 未结 5 1194
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 02:41

I\'m reading the Google Maps API and it states that the:

\"callback: The function to call once the script has loaded. If using the Auto-loading feature, this         


        
相关标签:
5条回答
  • 2020-12-07 03:29

    Well, perhaps what that bit of documentation means to say is that the "name" it expects should be a string containing the name of a function, instead of a "bare" function name (which is a reference to a function) or a function instantiation/definition expression.

    edit OK I see what the deal is. This really isn't a Google Maps thing, it's the Google Javascript loader toolkit. The API does indeed want a string, which makes perfect sense since the function you want to call is inside the code that you're loading, and therefore you can't have a reference to it from the calling environment.

    google.load("feeds", "1", {"callback" : "someFunctionName"});
    

    It would make no sense to write:

    google.load("feeds", "1", {"callback" : someFunctionName});
    

    because "someFunctionName" used like that — as a reference to something — could not possibly be a reference to the right function (if it's defined at all).

    0 讨论(0)
  • 2020-12-07 03:30

    Nothing.

    // f1 :: function name
    function f1(a) { return a*a; }  
    
    // f2 :: reference to an anonymous function
    var f2 = function(a) { return a*a; }  
    
    // f3 :: a reference to the first function
    var f3 = f1; 
    
    // these are equivalent. The second one calls into 
    // a different actual function. 
    f1(3);  // 9
    f2(4);  // 16
    f3(5);  // 25
    
    0 讨论(0)
  • 2020-12-07 03:38

    function: function func() {}

    function reference: func

    function name: 'func'

    0 讨论(0)
  • 2020-12-07 03:41

    A function name is a string ("alert"). A function reference is the function itself (alert).

    0 讨论(0)
  • 2020-12-07 03:41

    The name of a function is a string such as 'foo' in this case:

    function foo() {}
    

    A reference to a function is any variable that is set to the value of the function itself (not the result of calling it).

    Functions in Javascript can be anonymous - you can have a reference to a function that has no name.

    var bar = function() {}
    
    0 讨论(0)
提交回复
热议问题