Curious Behavior

Curious Behavior

A junior member of my team was just implementing something that requires a string in the configuration (doesn't allow a callback at execution time), and he discovered a curious behavior with the way the document ready event can be used that I could not explain.  The issue revolves around function execution scheduling, such as when the document ready event gets fired based on the placement of the $(function() { ... }) call.

In the two jsfiddles below, the $(document).on('ready', ....) call is made in the working sample via a callback for an AJAX execution (in the fiddle I use a setTimeout ti simulate a similar situation).  In the non-working sample, the on ready is used as the closure for execution.  Aside from the placement of the on ready, the code samples are identical.


What exactly causes the TestFn function to be defined in the functional sample, but it doesn't get defined in time for the non-functional sample?

(Edit: This is not a plugin issue, not sure why moderation put it in Plugins)