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 must specify a function name, not a function reference.
What's the difference been a JavaScript function name vs a function reference?
-
Can you provide a link to exactly where that documenation fragment is? Is it actually for the API itself, or is it for the Google javascript loader, or what? edit oh I found it.Pointy– Pointy2010年05月11日 18:10:23 +00:00Commented May 11, 2010 at 18:10
5 Answers 5
function: function func() {}
function reference: func
function name: 'func'
Comments
A function name is a string ("alert"). A function reference is the function itself (alert).
5 Comments
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() {}
Comments
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
1 Comment
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).