Safari and Non-Anonymous Function Passing
In JavaScript, you can pass functions to other functions as a parameter-the same way you would any other object.
So you can do something like this:
function getName()
{
return (name);
}
function addFunction(MyFunction)
{
...
}
var GetNameFunction = getName;
addFunction(GetNameFunction);
In the above we assign a variable to the function getName and then pass it as a parameter to an function called addFunction. But we don't really need the intermediary step of assigning a variable-we can pass the function inline.
function addFunction(MyFunction)
{
...
}
addFunction(function getName()
{
return (name);
});
Naming the function is sort of irrelevant, since the function is assigned to a variable at the receiving function(in this case, the MyFunction variable in addFunction()).
While this will work as is in Firefox and Internet Explorer, Safari's JavaScript parser will choke with a syntax error. Inline functions passed as parameters in Safari must be anonymous. See the corrected code below:
function addFunction(MyFunction)
{
...
}
addFunction(function()
{
return (name);
});
Just wanted to mention this before moving on to the improvements to the getElementsByClass I wrote about last time.
No comments :
Post a Comment