The behavior of \"this\" when function bar is called is baffling me. See the code below. Is there any way to arrange for \"this\" to be a plain old js object in
bar
You may use Function.apply on the function to set what this should refer to:
Function.apply
this
$("#thing").after($("click me").click(function() { barf.apply(document); // now this refers to the document });