jQuery $.proxy()วิธี การ
ตัวอย่าง
บังคับใช้บริบทของฟังก์ชัน "ทดสอบ" ภายใน objPerson:
$("button").click($.proxy(objPerson, "test"));
ความหมายและการใช้งาน
วิธี $.proxy ใช้ฟังก์ชันที่มีอยู่แล้วส่งคืนฟังก์ชันใหม่ที่มีบริบทเฉพาะ
วิธีนี้มักใช้สำหรับแนบเหตุการณ์กับองค์ประกอบที่บริบทชี้กลับไปที่วัตถุอื่น
เคล็ดลับ:หากคุณผูกฟังก์ชันที่ส่งคืนจาก $.proxy jQuery จะยังคงยกเลิกการเชื่อมโยงฟังก์ชันที่ถูกต้องหากส่งผ่านต้นฉบับ
ไวยากรณ์ 1
$(selector).proxy(function,context)
ไวยากรณ์2
$(selector).proxy(context,name)
Parameter | Description |
---|---|
function | The existing function to be called |
context | The name of the object where the function lies |
name | The existing function whose context will be changed (should be a property of the context object). |