jQuery/JavaScript “this” pointer confusion

前端 未结 7 697
余生分开走
余生分开走 2020-11-29 02:44

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

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 03:05

    You may use Function.apply on the function to set what this should refer to:

    $("#thing").after($("
    click me
    ").click(function() { barf.apply(document); // now this refers to the document });

提交回复
热议问题