Java Reference
In-Depth Information
}
} else {
ridePony = function(){
return "Riding on a standard pony is only okay";
}
}
return ride();
}
After we've checked whether the window.unicorn object exists (by checking to see if
it's truthy), we've rewritten the ride() function according to the outcome. Right at the
end of the function, we call it again so that the rewritten function is now invoked and the
relevant value returned. The downside is that the function is invoked twice the first time
it's used, although it becomes more efficient each subsequent time it's invoked. Let's take
a look at how it works:
ride(); // the function rewrites itself, then calls itself
<< "Riding on a bog standard horse is only okay"
Once the function has been invoked once, it's rewritten based on the browser's capabilities.
We can check this by inspecting the function without invoking it:
ride;
<< function (){
return "Riding on a bog standard horse is only okay"
}
This is a very useful pattern to initialize functions the first time they're called, optimizing
them for the browser in use.
Search WWH ::




Custom Search