什么是JavaScript“代理模式"? [英] What is a JavaScript "Proxy Pattern"?

查看:62
本文介绍了什么是JavaScript“代理模式"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天在jQuery.com上遇到了代理模式"的概念,但是什么也做不了.显然,它很有用,但我根本不理解这个主意,对我来说听起来有些陌生.有人可以简单地向我解释一下,好像我是3岁"吗?

I've come across the notion of 'Proxy Pattern' today on jQuery.com, but could not make anything of it. Apparently it is of great use, but I do not understand the idea at all, it sounds alien to me. Could someone please explain it to me in simple terms, "as if I were a 3 year old"?

推荐答案

想象一下,您的站点中有许多ajax请求.设计有所变化.现在,在每个请求之前,您要显示一些自定义加载gif.您需要在有ajax请求的地方更改所有代码,也可以使用代理模式.

Imagine you have site with many ajax requests. There is a change in design. Now before each request you want to display some custom loading gif. You neeed to change all the code where there is an ajax request or you can use proxy pattern.

var proxied = jQuery.ajax; // Preserving original function
jQuery.ajax = function() { 
    jQuery("#loading").dialog({modal: true});
    return proxied.apply(this, arguments);
}

这篇关于什么是JavaScript“代理模式"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆