<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Architecting Life &#187; Arguments</title>
	<atom:link href="http://xujiwei.com/blog/tags/arguments/feed/" rel="self" type="application/rss+xml" />
	<link>http://xujiwei.com/blog</link>
	<description>Just do it</description>
	<lastBuildDate>Wed, 28 Dec 2011 02:06:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>使用 arguments.caller 实现自动回调</title>
		<link>http://xujiwei.com/blog/autocallback-by-arguments-caller/</link>
		<comments>http://xujiwei.com/blog/autocallback-by-arguments-caller/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 01:36:31 +0000</pubDate>
		<dc:creator>Xu Jiwei</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[Arguments]]></category>
		<category><![CDATA[Callback]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://tmp.xujiwei.com/blog/?p=141</guid>
		<description><![CDATA[<a href="http://xujiwei.com/blog/autocallback-by-arguments-caller/" title="使用 arguments.caller 实现自动回调"></a>这是一篇介绍类似于 js hack 的文章，只是说明了一种可行的途径，并且可能会增加代码复杂程度，具体项目中是否可以使用还请自辨：） 在前端开发过程中，有许多业务流程可能是需要用户进行登录的，并且登录过程是放在弹出层中，这样就可以不用刷新页面，增强用户体验。在登录时，用户的操作就会被打断，为了进一步增强用户体验，我们可能需要在登录完成后自动继续进行用户在登录前想进行的操作。 假设有这样一个场景，用户需要发表一个留言，但是发表留言是需要登录的，而发表留言的输入框是一直显示的，这也就要求在用户点击了发表按钮时对用户登录状态进行验证，传统的做法是将在用户登录状态检查封装在一个函数之中，这个函数接收一个回调参数，如果登录验证通过，则执行回调函数。这样的逻辑可以用以下代码表示： 程序代码：[ 复制代码到剪贴板 ] function doAction() { checkLogin(function() { // 处理业务逻辑 }); } function checkLogin(callback) { if (isLogin) { callback(); } else { showLogin(callback); } } function showLogin(callback) { document.getElementById(&#8220;login-btn&#8221;).onclick = function() { isLogin = true; callback(); }; } 总觉得这样的方式会将业务逻辑放到一个匿名函数中，而不是放在了按钮的事件响应函数中，感觉上不是那么好。这对整个事件响应函数的改动比较大，主要的业务逻辑都放在了登录验证函数的回调中。而希望可以是下面这样： 程序代码：[ 复制代码到剪贴板 ] function doAction() { // 直接在函数开始进行登录验证，将业务逻辑独立出来 if (!doLogin()) { return; } // 处理业务逻辑 } function doLogin() { if (isLogin) { return true; } else { var callback = function() {}; // 自动组装 callback &#8230;<p class="read-more"><a href="http://xujiwei.com/blog/autocallback-by-arguments-caller/">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<a href="http://xujiwei.com/blog/autocallback-by-arguments-caller/" title="使用 arguments.caller 实现自动回调"></a><div class="post-content">

这是一篇介绍类似于 js hack 的文章，只是说明了一种可行的途径，并且可能会增加代码复杂程度，具体项目中是否可以使用还请自辨：）

在前端开发过程中，有许多业务流程可能是需要用户进行登录的，并且登录过程是放在弹出层中，这样就可以不用刷新页面，增强用户体验。在登录时，用户的操作就会被打断，为了进一步增强用户体验，我们可能需要在登录完成后自动继续进行用户在登录前想进行的操作。

假设有这样一个场景，用户需要发表一个留言，但是发表留言是需要登录的，而发表留言的输入框是一直显示的，这也就要求在用户点击了发表按钮时对用户登录状态进行验证，传统的做法是将在用户登录状态检查封装在一个函数之中，这个函数接收一个回调参数，如果登录验证通过，则执行回调函数。这样的逻辑可以用以下代码表示：
<div class="codeHead">程序代码：<a href="javascript:CopyText($('CODE_4316'));">[ 复制代码到剪贴板 ]</a></div>
<blockquote>
<div id="CODE_4316" class="codeMain"><span style="font-style: normal;">function doAction() {
checkLogin(function() {
// 处理业务逻辑
});
}
function checkLogin(callback) {
if (isLogin) {
callback();
} else {
showLogin(callback);
}
}
function showLogin(callback) {
document.getElementById(&#8220;login-btn&#8221;).onclick = function() {
isLogin = true;
callback();
};
}</span>
<ul></ul>
</div></blockquote>
总觉得这样的方式会将业务逻辑放到一个匿名函数中，而不是放在了按钮的事件响应函数中，感觉上不是那么好。这对整个事件响应函数的改动比较大，主要的业务逻辑都放在了登录验证函数的回调中。而希望可以是下面这样：
<div class="codeHead">程序代码：<a href="javascript:CopyText($('CODE_4415'));">[ 复制代码到剪贴板 ]</a></div>
<blockquote>
<div id="CODE_4415" class="codeMain"><span style="font-style: normal;">function doAction() {
// 直接在函数开始进行登录验证，将业务逻辑独立出来
if (!doLogin()) {
return;
}
// 处理业务逻辑
}
function doLogin() {
if (isLogin) {
return true;
} else {
var callback = function() {}; // 自动组装 callback
showLogin(callback);
return false;
}
}
function showLogin(callback) {
document.getElementById(&#8220;login-btn&#8221;).onclick = function() {
isLogin = true;
callback();
};
}</span>
<ul></ul>
</div></blockquote>
这里的关键在于怎么“自动组装 callback”，很幸运的是，JavaScript 的 Function 提供了一个属性 caller 可以用来获取调用当前函数的函数是什么。并且还可以用 caller 的 arguments 属性来获取 caller 执行时的参数是什么，这也就使我们自动组装 callback 并恢复 caller 的原参数成为可能。
<blockquote>
<p style="text-align: auto;"></p>

<span style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', Arial, Helvetica, Sans, FreeSans, Jamrul, Garuda, Kalimati; color: #000000;">
<div id="_mcePaste">
<div id="_mcePaste"><span style="font-style: normal;">&lt;div id=&#8221;result&#8221;&gt;&lt;/div&gt;</span></div>
<div id="_mcePaste"><span style="font-style: normal;">&lt;input type=&#8221;button&#8221; value=&#8221;Sumit&#8221; onclick=&#8221;sendPost()&#8221; id=&#8221;post&#8221; /&gt;</span></div>
<div id="_mcePaste"><span style="font-style: normal;">&lt;input type=&#8221;button&#8221; value=&#8221;Clear login&#8221; onclick=&#8221;isLogin=false&#8221; id=&#8221;reset&#8221; /&gt;</span></div>
<div id="_mcePaste"><span style="font-style: normal;">&lt;div id=&#8217;login&#8217; style=&#8221;border:1px solid #000;padding:10px;margin:10px;display:none&#8221;&gt;</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> &lt;strong&gt;Login&lt;/strong&gt;&lt;br /&gt;</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> &lt;input type=&#8221;button&#8221; id=&#8217;loginbtn&#8217; value=&#8221;Login&#8221; /&gt;</span></div>
<div id="_mcePaste"><span style="font-style: normal;">&lt;/div&gt;</span></div>
<div id="_mcePaste"><span style="font-style: normal;">&lt;script type=&#8221;text/javascript&#8221;&gt;</span></div>
<div id="_mcePaste"><span style="font-style: normal;">// 是否登录</span></div>
<div id="_mcePaste"><span style="font-style: normal;">var isLogin = false;</span></div>
<div id="_mcePaste"><span style="font-style: normal;">// 检查登录状态</span></div>
<div id="_mcePaste"><span style="font-style: normal;">var checkLogin = function(cfg) {</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> if (isLogin) {</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> return true;</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> }</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> cfg = cfg || {};</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> var callback = null;</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> // 自动执行回调</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> if (cfg.autoCallback &amp;&amp; arguments.callee.caller) {</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> try {</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> var caller  = arguments.callee.caller,</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> args    = caller.arguments || [],</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> scope   = cfg.callbackScope || {},</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> newArgs = [];</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> for (var i = 0; i &lt; args.length; ++i) {</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> newArgs.push(args[i]);</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> }</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> callback = caller ? function() {</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> try {</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> caller &amp;&amp; caller.apply(scope, newArgs);</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> } catch (e) {</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> //alert(e.message);</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> }</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> } : null;</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> } catch (e) {</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> callback = null;</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> }</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> }</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> </span></div>
<div id="_mcePaste"><span style="font-style: normal;"> showLogin(callback);</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> </span></div>
<div id="_mcePaste"><span style="font-style: normal;"> return false;</span></div>
<div id="_mcePaste"><span style="font-style: normal;">};</span></div>
<div id="_mcePaste"><span style="font-style: normal;">function sendPost() {</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> if (!checkLogin({autoCallback:true})) {</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> return;</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> }</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> </span></div>
<div id="_mcePaste"><span style="font-style: normal;"> document.getElementById(&#8216;result&#8217;).innerHTML += &#8216;Hello&lt;br /&gt;&#8217;;</span></div>
<div id="_mcePaste"><span style="font-style: normal;">}</span></div>
<div id="_mcePaste"><span style="font-style: normal;">function showLogin(callback) {</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> document.getElementById(&#8216;login&#8217;).style.display = &#8216;;</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> document.getElementById(&#8216;loginbtn&#8217;).onclick = function() {</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> document.getElementById(&#8216;login&#8217;).style.display = &#8216;none&#8217;;</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> isLogin = true;</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> callback &amp;&amp; callback();</span></div>
<div id="_mcePaste"><span style="font-style: normal;"> }</span></div>
<div id="_mcePaste"><span style="font-style: normal;">}</span></div>
<div id="_mcePaste"><span style="font-style: normal;">&lt;/script&gt;</span></div>
</div>
</span></blockquote>
其实弄出这么个方法来实现登录自动回调也是为了方便写代码，不用每次去将一堆业务逻辑放到 callback 中，直接在函数开始回一个 if (!checkLogin({autoCallback:true})) 就行。

caller 属性在 MDC 中被标记为“非标准”的，但是在主流浏览器中，都是支持这个属性的，也就是说，在浏览器兼容性上使用 caller 是没有问题的。

不过，使用奇技淫巧容易伤身体，慎用：）

</div>
]]></content:encoded>
			<wfw:commentRss>http://xujiwei.com/blog/autocallback-by-arguments-caller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

