$.proxy()
(runoob.com)
$(document).ready(function(){
test=function()
{
this.txt=" ";
$("div").click($.proxy(this.myClick,this));
//$("div").click(this.myClick);
};
test.prototype.myClick = function(event)
{
alert(this.txt);
alert(event.currentTarget.nodeName);
};
var x = new test();
});
div 。
우리가 사용하면.proxy () 가this에 전송될 때,this가 전송됩니다. 이것은this가test이고,this를 호출합니다.text 때도 받을 수 있습니다.하지만 사용하지 않습니다.proxy 시this가 가리키는 것은div입니다.
(runoob.com)
$(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'));
// $("button").click(objPerson.test);
});
$을(를) 사용하는 이유proxy(objPerson,'test')는 직접 사용할 때 테스트 방법의this가button이기 때문이다.
여기서도 한 대상이 이 리셋 함수를 실행할 때,this는 이 대상을 가리킨다. 설령 이 함수가 다른 대상에 쓰인 방법이라고 해도,this는 이 방법이 그 대상을 호출하는 것을 가리킨다.