加载中...

jQuery.proxy()


概述    jQuery.proxy( function, context )

返回值:Function

描述:接受一个函数,然后返回一个新函数,并且这个新函数始终保持了特定的上下文语境。

  • V : 1.4jQuery.proxy( function, context )

    • function
      类型: Function()
      将要改变上下文语境的函数。
    • context
      类型: PlainObject
      函数的上下文语境(this)会被设置成这个 object 对象。
  • V : 1.4jQuery.proxy( context, name )

    • context
      类型: PlainObject
      函数的上下文语境会被设置成这个 object 对象。
    • name
      类型: String
      将要改变上下文语境的函数名(这个函数必须是前一个参数 context 对象的属性)。
  • V : 1.6jQuery.proxy( function, context [, additionalArguments ] )

    • function
      类型: Function()
      将要改变上下文语境的函数。
    • context
      类型: PlainObject
      函数的上下文语境会被设置成这个 object 对象。
    • additionalArguments
      类型: Anything
      任何数目的参数传递给function参数的函数引用。
  • V : 1.6jQuery.proxy( context, name [, additionalArguments ] )

    • context
      类型: PlainObject
      函数的上下文语境会被设置成这个 object 对象。
    • name
      类型: String
      将要改变上下文语境的函数名(这个函数必须是前一个参数 context 对象的属性)。
    • additionalArguments
      类型: Anything
      任何数目的参数传递给name参数中命名的函数。

这个方法通常在向一个元素上附加事件处理函数时,上下文语境实际是指向另一个对象的情况下使用。另外,jQuery 能够确保即使你绑定的函数是经过 jQuery.proxy() 处理过的函数,你依然可以用原先的函数来正确地取消绑定。

要知道,jQuery的事件绑定子系统为每一个事件处理函数分配一个唯一的ID 用于对其进行跟踪,这样的话,当需要解除绑定特定的事件处理时,系统就知道该解除绑定哪个事件处理函数了。jQuery.proxy()参数中的 function 会被事件子系统当成一个单独的函数,即使是当它用于绑定不同的上下文时。为了避免错误的解除绑定事件,可以在绑定或解除绑定时,使用一个唯一的事件命令空间(例如, "click.myproxy1"),这样比在解除绑定时仅指定被代理函数要好。

从jQuery 1.6开始, 任意数量的附加参数可以提供给$.proxy(),并且它们将被传递给那些上下文将被改变的函数。

从jQuery 1.9开始, 当contextnullundefined,代理函数将通过this对象被调用。(手册网注:也就是相当于contextthis对象)。允许$.proxy()应用一个函数的部分参数,而不改变上下文。

示例

实例

修改使用参数为"function, context"的jQuery.proxy()方法绑定的点击事件的上下文语境。并且在第一次点击事件执行后,解除原先的绑定。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="http://code.jquery.com/jquery-latest.js"></script>
  5. </head>
  6. <body>
  7. <p><button type="button" id="test">Test</button></p>
  8. <div id="log"></div>
  9. <script>
  10. var me = {
  11. type: "zombie",
  12. test: function(event) {
  13. /* Without proxy, `this` would refer to the event target */
  14. /* use event.target to reference that element. */
  15. var element = event.target;
  16. $(element).css("background-color", "red");
  17. /* With proxy, `this` refers to the me object encapsulating */
  18. /* this function. */
  19. $("#log").append( "Hello " + this.type + "<br>" );
  20. $("#test").unbind("click", this.test);
  21. }
  22. };
  23. var you = {
  24. type: "person",
  25. test: function(event) {
  26. $("#log").append( this.type + " " );
  27. }
  28. };
  29. /* execute you.test() in the context of the `you` object */
  30. /* no matter where it is called */
  31. /* i.e. the `this` keyword will refer to `you` */
  32. var youClick = $.proxy( you.test, you );
  33. /* attach click handlers to #test */
  34. $("#test")
  35. /* this === "zombie"; handler unbound after first click */
  36. .on( "click", $.proxy( me.test, me ) )
  37. /* this === "person" */
  38. .on( "click", youClick )
  39. /* this === "zombie" */
  40. .on( "click", $.proxy( you.test, me ) )
  41. /* this === "<button> element" */
  42. .on( "click", you.test );
  43. </script>
  44. </body>
  45. </html>

运行一下

实例

通过调用参数为"context, function name"的jQuery.proxy()方法,强制修改函数的上下文语境。 并且在第一次点击事件执行后,解除绑定。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="http://code.jquery.com/jquery-latest.js"></script>
  5. </head>
  6. <body>
  7. <p><button id="test">Test</button></p>
  8. <p id="log"></p>
  9. <script>
  10. var obj = {
  11. name: "John",
  12. test: function() {
  13. $("#log").append( this.name );
  14. $("#test").off("click", obj.test);
  15. }
  16. };
  17. $("#test").on( "click", jQuery.proxy( obj, "test" ) );
  18. </script>
  19. </body>
  20. </html>

运行一下

实例

更改绑定点击处理程序函数的上下文。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="http://code.jquery.com/jquery-latest.js"></script>
  5. </head>
  6. <body>
  7. <p><button type="button" id="test">Test</button></p>
  8. <div id="log"></div>
  9. <script>
  10. var me = {
  11. /* I'm a dog */
  12. type: "dog",
  13. /* Note that event comes *after* one and two */
  14. test: function(one, two, event) {
  15. $("#log")
  16. /* `one` maps to `you`, the 1st additional */
  17. /* argument in the $.proxy function call */
  18. .append( "<h3>Hello " + one.type + ":</h3>" )
  19. /* the `this` keyword refers to `me` */
  20. /*(the 2nd, context, argument of $.proxy) */
  21. .append( "I am a " + this.type + ", " )
  22. /* `two` maps to `they`, the 2nd additional */
  23. /* argument in the $.proxy function call */
  24. .append( "and they are " + two.type + ".<br>" )
  25. /* the event type is "click" */
  26. .append( "Thanks for " + event.type + "ing " )
  27. /* the clicked element is `event.target`, */
  28. /* and its type is "button" */
  29. .append( "the " + event.target.type + "." );
  30. }
  31. };
  32. var you = { type: "cat" };
  33. var they = { type: "fish" };
  34. /* Set up handler to execute me.test() in the context */
  35. /* of `me`, with `you` and `they` as additional arguments */
  36. var proxy = $.proxy( me.test, me, you, they );
  37. $("#test")
  38. .on( "click", proxy );
  39. </script>
  40. </body>
  41. </html>

运行一下


还没有评论.