jQuery中的$.proxy官方描述为:

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

官方API;

jQuery.proxy( function, context )

function为执行的函数,content为函数的上下文this值会被设置成这个object对象

jQuery.proxy( context, name )

content 函数的上下文会被设置成这个object对象,name要执行的函数,次函数必须是content对象的属性、

jQuery.proxy( function, context [, additionalArguments ] )

function为执行的函数,content为函数的上下文this值会被设置成这个object对象,additionalArguments任何数目的参数,传递给function

更详细的用法参考:

http://www.css88.com/jqapi-1.9/jQuery.proxy/

现在让我们一起来看实际例子:

var objPerson = {
name: "obj",
age: 32,
test: function() {
$("p").after("Name: " + this.name + "<br> Age: " + this.age);
}
} $("#btn").on("click", $.proxy(objPerson.test, objPerson))

点击按钮,输出:Name:obj  Age:32

objPerson.test表示上下文的方法,objPerson代表执行的上下文,例子中的this的上下文指的是objPerson

更具体的例子如:

var me = {
type: "zombie",
test: function(event) {
/* Without proxy, `this` would refer to the event target */
/* use event.target to reference that element. */
var element = event.target;
$(element).css("background-color", "red"); /* With proxy, `this` refers to the me object encapsulating */
/* this function. */
$("#log").append("Hello " + this.type + "<br>");
$("#test").unbind("click", this.test);
}
}; var you = {
type: "person",
test: function(event) {
$("#log").append(this.type + " ");
}
}; /* execute you.test() in the context of the `you` object */
/* no matter where it is called */
/* i.e. the `this` keyword will refer to `you` */
var youClick = $.proxy(you.test, you); /* attach click handlers to #test */
$("#test") /* this === "zombie"; handler unbound after first click */
.on("click", $.proxy(me.test, me)) /* this === "person" */
.on("click", youClick) /* this === "zombie" */
.on("click", $.proxy(you.test, me)) /* this === "<button> element" */
.on("click", you.test);

结合以上说明,再写一个综合的例子,例如 js封装一个ajax请求,代码如下:

var t = {
param: {},
url: "",
type: "get",
dataType: "json",
ajaxOnly: true,
contentType: 'application/x-www-form-urlencoded',
/**
* 取model数据
* @param {Function} onComplete 取完的回调函
* 传入的第一个参数为model的数第二个数据为元数据,元数据为ajax下发时的ServerCode,Message等数
* @param {Function} onError 发生错误时的回调
* @param {Boolean} ajaxOnly 可选,默认为false当为true时只使用ajax调取数据
* @param {Boolean} scope 可选,设定回调函数this指向的对象
* @param {Function} onAbort 可选,但取消时会调用的函数
*/
execute: function(onComplete, onError, ajaxOnly, scope) {
var __onComplete = $.proxy(function(data) {
var _data = data;
if (typeof data == 'string') _data = JSON.parse(data); // @description 对获取的数据做字段映射
var datamodel = typeof this.dataformat === 'function' ? this.dataformat(_data) : _data; if (this.onDataSuccess) this.onDataSuccess.call(this, datamodel, data);
if (typeof onComplete === 'function') {
onComplete.call(scope || this, datamodel, data);
} }, this); var __onError = $.proxy(function(e) {
if (typeof onError === 'function') {
onError.call(scope || this, e);
}
}, this); this.sendRequest(__onComplete, __onError); }, sendRequest: function(success, error) {
var params = _.clone(this.getParam() || {});
var crossDomain = {
'json': true,
'jsonp': true
}; if (this.type == 'POST') {
this.dataType = 'json';
} //jsonp与post互斥
$.ajax({
url: this.url,
type: this.type,
data: params,
dataType: this.dataType,
contentType: this.contentType,
crossDomain: crossDomain[this.dataType],
timeout: 50000,
// xhrFields: {
// withCredentials: true
// },
success: function(res) {
success && success(res);
},
error: function(err) {
error && error(err);
}
});
}, setParam: function(key, val) {
if (typeof key === 'object') {
_.extend(this.param, key);
} else {
this.param[key] = val;
}
}, removeParam: function(key) {
delete this.param[key];
}, getParam: function() {
return this.param;
}, dataformat: function(data) {
if (_.isString(data)) data = JSON.parse(data);
if (data.data) return data.data;
return data;
}, onDataSuccess: function() {}
}

调用封装的方法:

function getData(url) {
//设置参数
t.setParam({
"CurrentDestId": 7,
"Platform":2,
"ViewDestId":7
});
t.url=url;
t.type="post"; //调用
t.execute(function(data){
var list=data.Tags;
var temp=_.template($("#foodListTemp").html());
$("#list").html(temp({"dataTag":list}));
},function(data){
alert("fail");
});
}

随机推荐

  1. Linux(CentOS)系统下搭建svn服务器

    由于GitHub的私有项目需要收费,gitlab对服务器的要求必须是4GB内存以上.对于一些个人的小型项目,想要免费的版本控制工具来管理自己的代码,又不想代码公开,无疑SVN是比较好的选择.windo ...

  2. 深入理解Nginx

    nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ...

  3. luarocks模块管理工具

    1.简介 该软件包可以安装和更新lua的第三方模块. 2.下载地址 请在 http://luarocks.org/releases/ 页面选择需要的软件包. wget http://luarocks. ...

  4. NPOI+反射+自定义特性实现上传excel转List及验证

    1.自定义特性 [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public ...

  5. Ubuntu 添加用户到 sudoer

    一.概述 新建用户后,我们可能需要该用户能够使用一些越权的东西.sudo命令能够暂时提升该用户的权限到root,但是前提是要求该用户存在与 sudoer list 中. sudoers 存储在 /et ...

  6. ZOJ Monthly, March 2018 Solution

    A - Easy Number Game 水. #include <bits/stdc++.h> using namespace std; #define ll long long #de ...

  7. JS事件监听手机屏幕触摸事件 Touch

    JS移动客户端--触屏滑动事件 移动端触屏滑动的效果其实就是图片轮播,在PC的页面上很好实现,绑定click和mouseover等事件来完成.但是在移动设备上,要实现这种轮播的效果,就需要用到核心的t ...

  8. 浅谈padding

    浅谈padding padding是CSS盒子模型的一部分,代表盒子模型的内边距. 用法 padding属性有四个值,分别代表上.右.下.左的内边距. .box { padding: 10px 5px ...

  9. android自定义Activity窗口大小(theme运用)

    http://gundumw100.iteye.com/blog/906195 正常情况下,我们开发的应用程序都会上占满整个屏幕,那么怎么样才能开发出自定义窗口大小的的程序呢?如下图所示: 实现起来非 ...

  10. pyDay10

    内容来自廖雪峰的官方网站. 1.python的赋值语句:a, b, c = x, y, z 相当于 a = x, b = y, c = z.(事实上等式右边是一个tuple) 2.获得genarato ...