惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

www.infosecurity-magazine.com
www.infosecurity-magazine.com
D
DataBreaches.Net
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
F
Full Disclosure
V2EX - 技术
V2EX - 技术
N
News and Events Feed by Topic
Help Net Security
Help Net Security
L
LangChain Blog
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
Google Online Security Blog
Google Online Security Blog
P
Proofpoint News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
B
Blog RSS Feed
N
Netflix TechBlog - Medium
N
News | PayPal Newsroom
TaoSecurity Blog
TaoSecurity Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Vulnerabilities – Threatpost
B
Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
I
Intezer
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
AI
AI
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyberwarzone
Cyberwarzone
P
Proofpoint News Feed
Google DeepMind News
Google DeepMind News
G
GRAHAM CLULEY
Vercel News
Vercel News
罗磊的独立博客
MyScale Blog
MyScale Blog
Last Week in AI
Last Week in AI
博客园 - 司徒正美
C
CERT Recently Published Vulnerability Notes
GbyAI
GbyAI
Scott Helme
Scott Helme
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Troy Hunt's Blog
A
About on SuperTechFans
P
Privacy International News Feed

博客园 - 朱峰(Peter.zhu)

[转]Becoming a JavaScript ninja 【转载】关于大型asp.net应用系统的架构—如何做到高性能高可伸缩性 【javascript】Lazy Load, 延迟加载图片的 jQuery 插件 【译】15个必须知道的chrome开发者技巧 Implementing multi-level trees in MS SQL Server 【转】Linq to EF 与Linq to Object 使用心得 Implementing the Singleton Pattern in C# 【转】【翻译】The Top 10 Mistakes That KnockoutJS Developers Make 【转载】net的nuget无法更新解决 c# ASP.Net 使用开源免费类库操作Excel VISUAL STUDIO 使用技巧大全 中英文版本之 一 - 命名空间引用管理 Redirect and Post JSON object in ASP.NET MVC 【引】Difference between Asp.Net WebForm and Asp.Net MVC 【引】How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC 【引】Version Control System - SVN - Deployments Best Practices 【引】Version Control System - SVN - Developing and Deploying with Branches ASP.NET之ORM模型 - ActiveRecord 模式 【转】Domain Driven Design - Clear Your Concepts Before You Start 【转】单体模式-经典实现 Implementing the Singleton Pattern in C#
Javascript基础复习 - jQuery Proxy函数
朱峰(Peter.zhu) · 2013-12-25 · via 博客园 - 朱峰(Peter.zhu)

2013-12-25 14:42  朱峰(Peter.zhu)  阅读(493)  评论()    收藏  举报

API 文档 http://api.jquery.com/jQuery.proxy/ 

jQuery.proxy(function,obj)

jquery1.4新增,返回一个新函数,并且这个函数始终保持了特定的作用域。

当有事件处理函数要附加到元素上,但他们的作用域实际是指向另一个对象时,这个方法最有用了。此外,最妙的是,jQuery能够确保即便你绑定的函数是经过jQuery.proxy()处理过的函数,你依然可以传递原先的函数来准确无误地取消绑定。请参考下面的例子。

这个函数还有另一种用法,jQuery.proxy( scope, name
)。第一个参数是要设定的作用域对象。第二个参数是将要设置作用域的函数名(必须是第一个作用域对象的一个属性)。

看起来有点call/apply的意思是不是?

示例:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" class="Chrome">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>Title</title>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js" type="text/javascript"></script>

    <script type="text/javascript">
	
        $(function () {
		
			var obj = {
			  name: "John",
			  test: function() {
				alert( this.name );
				$("#test").unbind("click", obj.test);
			  }
			};
			 
			//$("#test").click( jQuery.proxy( obj, "test" ) );
			 
			// 以下代码跟上面那句是等价的:
			//$("#test").click( jQuery.proxy( obj.test, obj ) );
			 
			// 可以与单独执行下面这句做个比较。
			$("#test").click( obj.test );
			
		});
    </script>
	
</head>
<body>
	<div id="test">Click Here!</div>
</body>
</html>