<?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>Rainux's Journal&#187; Javascript Archives  &laquo; Rainux&#039;s Journal</title>
	<atom:link href="http://rainux.org/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://rainux.org</link>
	<description>Rubyist of Vimmer</description>
	<lastBuildDate>Tue, 10 Aug 2010 23:41:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>使用 Function.apply.apply() 为 IE 里的原生函数添加 .apply() 方法</title>
		<link>http://rainux.org/%e4%bd%bf%e7%94%a8-function-apply-apply-%e4%b8%ba-ie-%e9%87%8c%e7%9a%84%e5%8e%9f%e7%94%9f%e5%87%bd%e6%95%b0%e6%b7%bb%e5%8a%a0-apply-%e6%96%b9%e6%b3%95</link>
		<comments>http://rainux.org/%e4%bd%bf%e7%94%a8-function-apply-apply-%e4%b8%ba-ie-%e9%87%8c%e7%9a%84%e5%8e%9f%e7%94%9f%e5%87%bd%e6%95%b0%e6%b7%bb%e5%8a%a0-apply-%e6%96%b9%e6%b3%95#comments</comments>
		<pubDate>Sun, 13 Dec 2009 09:14:20 +0000</pubDate>
		<dc:creator>Rainux</dc:creator>
				<category><![CDATA[Programming 编程]]></category>
		<category><![CDATA[Web Development 织网]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://rainux.org/?p=246</guid>
		<description><![CDATA[IE 里有很多原生的 Javascript 函数实际上都不是一个标准的 Function 对象，例如 window.alert，window.setTimeout 以及 IE8 的 window.console.log 等。在需要对这样的函数进行包装的时候，会因为它们都没有 Function 对象应该有的 .apply() 及 .call() 方法而难以做到。 这段代码为了兼容旧版本没有 console 的浏览器，尝试将 console.log 包装为 $.log，但是基于上述原因它在 IE 里无法执行。 $ = {}; if (console &#38;&#38; console.log) { $.log = function() { console.log.apply(console, arguments); }; } else { $.log = function() {}; } 由于 window 和 console 这样的原生对象都是只读的，所以无法使用类似这样的代码简单地为其添加 .apply() [...]]]></description>
			<content:encoded><![CDATA[<p>IE 里有很多原生的 Javascript 函数实际上都不是一个标准的 Function 对象，例如 window.alert，window.setTimeout 以及 IE8 的 window.console.log 等。在需要对这样的函数进行包装的时候，会因为它们都没有 Function 对象应该有的 .apply() 及 .call() 方法而难以做到。</p>

<p>这段代码为了兼容旧版本没有 console 的浏览器，尝试将 console.log 包装为 $.log，但是基于上述原因它在 IE 里无法执行。</p>

<pre name="code" class="javascript">$ = {};

if (console &amp;&amp; console.log) {
    $.log = function() {
        console.log.apply(console, arguments);
    };
} else {
    $.log = function() {};
}
</pre>

<p>由于 window 和 console 这样的原生对象都是只读的，所以无法使用类似这样的代码简单地为其添加 .apply() 方法。</p>

<pre name="code" class="javascript">console.log.apply = Function.apply;
</pre>

<p>解决办法是使用 Function.apply.apply() 将 Function.apply 函数对象 apply 到 console.log 上。</p>

<pre name="code" class="javascript">$ = {};

if (console &amp;&amp; console.log) {
    $.log = function() {
        Function.apply.apply(console.log, [console, arguments]);
    };
} else {
    $.log = function() {};
}
</pre>

<p>这段代码和第一段代码完全等价。</p>

<p>如果觉得不容易理解，可以看看这个 .apply() 函数比较清晰的例子。这两行代码也完全等价。</p>

<pre name="code" class="javascript">[1, 2, 3, 4].slice(0, 2);
Array.prototype.slice.apply([1, 2, 3, 4], [0, 2]);
</pre>

	标签：<a href="http://rainux.org/tag/javascript/" title="Javascript" rel="tag">Javascript</a><br />

	<h4>相关日志</h4>
	<ul class="st-related-posts">
	<li>无相关日志</li>
	</ul>

]]></content:encoded>
			<wfw:commentRss>http://rainux.org/%e4%bd%bf%e7%94%a8-function-apply-apply-%e4%b8%ba-ie-%e9%87%8c%e7%9a%84%e5%8e%9f%e7%94%9f%e5%87%bd%e6%95%b0%e6%b7%bb%e5%8a%a0-apply-%e6%96%b9%e6%b3%95/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
