转载至:http://www.cnblogs.com/acles/archive/2012/11/20/2779282.html

JQuery.proxy(function,context):

使用context代替function中的context。

比如:

var you = {

  type: "person",

  test: function(event) {

    $("#log").append( this.type + " " );

  }

$("#test").click(you.test);调用这句只有相当于调用:

$("#test").click(function(event){

         $("#log").append( this.type + " " );

});

所以这里的this指的是$("#test").

如果这样调用:$("#test").click($.proxy(you.test,you));

此时的调用相当于:

$("#test").click(function(event){

$("#log").append( you.type + " " );

});

虽然调用事件的对象是$("#test"),但是却可以使用$.proxy把事件执行内的对象改变为you。

JQuery.proxy(context,functionname):

第一个参数是你想proxy的对象,第二个参数为要改变的函数的名字。

var obj = {

name: "John",

test: function() {

$("#log").append( this.name );

$("#test").unbind("click", obj.test);

}

};

$("#test").click( jQuery.proxy( obj, "test" ) );   把obj作为context传入test中,而不是$("#test").

这个执行完之后,结果会是John,

如果使用下面这句

$("#test").click(obj.test);

结果会是$("#test").的name值。

这个函数和上面的那个函数的功能一样,就是使用了更加简洁的方式。

最后附上一篇stackoverflow的一个关于Porxy的一个问答的答案。问答原地址:http://stackoverflow.com/questions/4986329/understanding-proxy-in-jquery

What it ultimately does is it ensures that the value of this in a function will be the value you desire.

A common example is in a setTimeout that takes place inside a click handler.

Take this:

$('#myElement').click(function() {

        // In this function, "this" is our DOM element.

    $(this).addClass('aNewClass');

});

The intention is simple enough. When myElement is clicked, it should get the class aNewClass. Inside the handler this represents the element that was clicked.

But what if we wanted a short delay before adding the class? We might use a setTimeout to accomplish it, but the trouble is that whatever function we give to setTimeout, the value of thisinside that function will be window instead of our element.

$('#myElement').click(function() {

    setTimeout(function() {

          // Problem! In this function "this" is not our element!

        $(this).addClass('aNewClass');

    }, 1000);

});

So what we can do instead, is to call $.proxy(), sending it the function and the value we want to assign to this, and it will return a function that will retain that value.

$('#myElement').click(function() {

   // ------------------v--------give $.proxy our function,

    setTimeout($.proxy(function() {

        $(this).addClass('aNewClass');  // Now "this" is again our element

    }, this), 1000);

   // ---^--------------and tell it that we want our DOM element to be the

   //                      value of "this" in the function

});

So after we gave $.proxy() the function, and the value we want for this, it returned a function that will ensure that this is properly set.

How does it do it? It just returns an anonymous function that calls our function using the .apply()method, which lets it explicitly set the value of this.

A simplified look at the function that is returned may look like:

function() {

    // v--------func is the function we gave to $.proxy

    func.apply( ctx );

    // ----------^------ ctx is the value we wanted for "this" (our DOM element)

}

So this anonymous function is given to setTimeout, and all it does is execute our original function with the proper this context.

个人对JQuery Proxy()函数的理解的更多相关文章

  1. 【转】个人对JQuery Proxy()函数的理解

    原文地址:http://www.cnblogs.com/acles/archive/2012/11/20/2779282.html JQuery.proxy(function,context): 使用 ...

  2. jQuery.proxy() 函数详解

    jQuery.proxy()函数用于改变函数的上下文. 你可以将指定函数传入该函数,该函数将返回一个新的函数,其执行代码不变,但函数内部的上下文(this)已经被更改为指定值. 该函数属于全局的jQu ...

  3. jQuery.proxy()函数

    jQuery.proxy(),接受一个函数,然后返回一个新函数,并且这个新函数始终保持了特定的上下文(context)语境.   context 代表上下文 name是上下文的某个属性 jQuery. ...

  4. 图片放大功能插件及jquery.extend函数理解

    前端时间,产品提出社区评论中的图片需要有放大功能.感觉可以共用,所以就想整合一个插件,过程中也借鉴了一些例子. 分析下自己的代码思路: var scaleImg = function(opts) { ...

  5. jQuery proxy详解

    第一次接触jQuery.proxy()时感觉这个方法不实用,不明白它到底是个什么意思.今天来将jQuery官网上的解释进行一下翻译,顺便添加自己的理解和一些示例.proxy也可称为代理. jQuery ...

  6. jQuery.proxy()的用法

    一:参考范文一 第一次接触jQuery.proxy()时感觉这个方法不实用,不明白它到底是个什么意思.今天来将jQuery官网上的解释进行一下翻译,顺便添加自己的理解和一些示例.proxy也可称为代理 ...

  7. jQuery工具函数(转)

    原文地址:http://www.cnblogs.com/kissdodog/archive/2012/12/27/2835561.html 作者:逆心 ------------------------ ...

  8. jQuery.ajax() 函数详解

    jQuery.ajax()函数用于通过后台HTTP请求加载远程数据. jQuery.ajax()函数是jQuery封装的AJAX技术实现,通过该函数,我们无需刷新当前页面即可获取远程服务器上的数据. ...

  9. jquery $.proxy使用

    在某些情况下,我们调用Javascript函数时候,this指针并不一定是我们所期望的那个.例如: //正常的this使用 $('#myElement').click(function() { // ...

随机推荐

  1. 揭秘史上最完美一步到位的搭建Andoriod开发环境

    Windows环境下Android开发环境搭建虽然不难而且网上资料众多,但是众多资料如出一折 忽略了很多细节,最终还是没能达到满意效果. 基本步骤如下:JDK安装.环境变量配置.Eclipse下载.A ...

  2. webpack模块依赖管理介绍

    http://webpack.github.io/docs/ webpack is a module bundler. 是一个模块管理器 webpack可以管理模块的依赖关系,并产生可以替代这些模块的 ...

  3. List 用法和实例(转载)

    写在粘贴复制前:英文的感觉也可以,也能看的懂,多看看英文资料没坏处的 Problem. You have questions about the List collection in the .NET ...

  4. 山东省第七届ACM省赛------Reversed Words

    Reversed Words Time Limit: 2000MS Memory limit: 131072K 题目描述 Some aliens are learning English. They ...

  5. phpMyAdmin的配置

    好久没写东西了,上来记录一下今天学的一点小东西吧~ 之前搞php开发的时候,一直用的是SQLyog来操作mysql数据库的,但是今天发现sqlyog功能不是很完善,主要是我想修改数据库名,但是sqly ...

  6. 93、持续集成以及Jenkins的知识介绍

    持续集成是什么? }持续集成源于极限编程(XP),是一种软件实践,软件开发过程中集成步骤是一个漫长并且无法预测的过程.集成过程中可能会爆发大量的问题,因此集成过程需要尽可能小而多,实际上持续集成讲的是 ...

  7. JavaScript使用技巧(1)——JS常用的函数

    1.字符串对象函数和属性 函数: charAt():返回在指定位置的字符. charCodeAt():返回在指定的位置的字符的 Unicode 编码. concat():连接字符串. indexOf( ...

  8. sp 数据导入导出

    https://msdn.microsoft.com/zh-cn/library/ee231568.aspxsharepoint 微软文档 sharepoint 导入导出分两种: 1.一个网站全部导入 ...

  9. Ubuntu学习小结(一)

    这段时间,抽空研究了一下Ubuntu,虽然也有过到目前为止使用计算机最作死的经历,但目前已经学会了一些最基本的操作.在这里简单的记录一下,算是吸取的教训,供其他人借鉴. 1.装Ubuntu系统.装Ub ...

  10. Android4.0.3 USB OTG底层插入上报过程分析(1)

    下面的两个宏是PM8058的MMP11(R15),MMP12(P15)管脚.#define EXT_CHG_VALID_MPP 10#define EXT_CHG_VALID_MPP_2 11 sta ...