强制执行 objPerson 内的 "test" 函数的上下文:

$("button").click($.proxy(objPerson,"test"));

语法 1

$(selector).proxy(function,context)

语法 2

$(selector).proxy(context,name)
function:要被带调用的函数
context:要被调用的函数所在的对象名称
name:属性
例如:
语法二例子:
$(document).ready(function(){
  var objPerson = {
    name: "John Doe",
    age: 32,
    test: function(){
      $("p").after("Name: " + this.name + "<br> Age: " + this.age);
    }
  };
  $("button").click($.proxy(objPerson,"test"));
});

 

posted on 2019-07-31 18:21  渐凸强、啊哈  阅读(131)  评论(0编辑  收藏  举报