jQuery $.proxy()

来源:互联网 发布:java流的作用 编辑:程序博客网 时间:2024/06/03 22:59

转载

javascript

/*** jQuery基础知识 proxy函数测试* http://stackoverflow.com/questions/4986329/understanding-proxy-in-jquery **/$(document).ready(function() {    var objPerson = {        name: "John Doe",        age: 32,        test: function() {            $("p").html("Name: " + this.name + "<br> Age: " + this.age);        }    };    $(".proxy").click($.proxy(objPerson, "test"));    $(".normal").click(objPerson.test);});

CSS

button {    border: 1px solid #333;    padding: 10px 15px;    background: transparent;}

html

<button class="proxy">Run test function with proxy</button><button class="normal">Run test function</button><p></p>

结论

点击class是proxy的按钮,可以出来name和age,normal则没有, $.proxy最终就是要保证你的function里面的this是你想要的

0 0