<?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>Shadow &#38; Honnix &#187; Javascript</title>
	<atom:link href="http://honnix.com/blog/archives/tag/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://honnix.com/blog</link>
	<description>she&#039;s growing up</description>
	<lastBuildDate>Sun, 15 Jan 2012 10:44:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>对于cheater-node的总结——underscore篇</title>
		<link>http://honnix.com/blog/archives/726</link>
		<comments>http://honnix.com/blog/archives/726#comments</comments>
		<pubDate>Sun, 24 Apr 2011 08:31:03 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[underscore]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=726</guid>
		<description><![CDATA[javascript确确实实是一个functional的语言，但问题是它本身并没有提供什么方便使用的库。不过还好我们有社区，有无数英雄们默默无闻地做贡献（眼泪花花儿的）。 介绍一下underscore。点过去看看就知道了，亲切得很。 安装很简单，直接npm就行了。不知道npm是什么的，或者google，或者参考这里。 虽然那里有无数的例子，但这里还是给一个吧： 1 2 3 4 5 6 7 8 9 10 11 12 13 var loginInfo = function &#40;&#41; &#123; var info = &#91; &#91;'tz_offset', '480'&#93;, &#91;'username', 'xxx'&#93;, &#91;'password', 'yyy'&#93;, &#91;'realm', 'Zoning'&#93; &#93;; var _ = require&#40;'underscore'&#41;; &#160; return _&#40;info&#41;.map&#40;function &#40;item&#41; &#123; return item&#91;0&#93; + '=' + item&#91;1&#93;; &#125;&#41;.join&#40;'&#38;amp;'&#41;; &#125;; “_(info)”返回一个对象，包含一个名叫_wrapped的property。 &#62;&#8230;]]></description>
			<content:encoded><![CDATA[<p>javascript确确实实是一个functional的语言，但问题是它本身并没有提供什么方便使用的库。不过还好我们有社区，有无数英雄们默默无闻地做贡献（眼泪花花儿的）。</p>
<p>介绍一下<a title="underscore" href="http://documentcloud.github.com/underscore/" target="_blank">underscore</a>。点过去看看就知道了，亲切得很。</p>
<p>安装很简单，直接npm就行了。不知道npm是什么的，或者google，或者参考<a title="npm安装东西时候的困惑" href="http://honnix.com/blog/archives/711">这里</a>。</p>
<p>虽然那里有无数的例子，但这里还是给一个吧：</p>

<div class="wp_codebox"><table><tr id="p7263"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p726code3"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> loginInfo <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> info <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'tz_offset'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'480'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'username'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'xxx'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'password'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'yyy'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'realm'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Zoning'</span><span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> _ <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'underscore'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> _<span style="color: #009900;">&#40;</span>info<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'='</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;amp;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>“_(info)”返回一个对象，包含一个名叫_wrapped的property。</p>
<pre>&gt; var _ = require('underscore');
&gt; var w = _([1, 2, 3]);
&gt; w
{ _wrapped: [ 1, 2, 3 ] }</pre>
<p>有了这个东西之后一切都变得美好了，跟所有functional的用法一样，点啊点得就干了很多事儿。不过需要注意的是_(info).map()之后返回的东西就是一个数组了。嗯，不太好……</p>
<p>这个问题人家早就考虑过了：</p>

<div class="wp_codebox"><table><tr id="p7264"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p726code4"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> loginInfo <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> info <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'tz_offset'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'480'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'username'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'xxx'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'password'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'yyy'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'realm'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Zoning'</span><span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> _ <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'underscore'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> _<span style="color: #009900;">&#40;</span>info<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">chain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'='</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;amp;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>先chain一下，玩儿完之后再value一把。这回爽了吧？</p>
<p>顺便说一下，functional这么好的东西为什么会有人跳出来反对呢？</p>
<p>再多废一句话啊。如果你在node的交互式终端里面使用“_”，你会发现诡异的问题，原因是这个终端默认使用“_”表示上一个命令的返回值。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/726/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>对于cheater-node的总结——继承篇</title>
		<link>http://honnix.com/blog/archives/722</link>
		<comments>http://honnix.com/blog/archives/722#comments</comments>
		<pubDate>Sun, 24 Apr 2011 07:58:31 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[EventEmitter]]></category>
		<category><![CDATA[inherit]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=722</guid>
		<description><![CDATA[javascript的对象模型诡异得很，我等半脑残几乎无法领会其精要，仅继承这件简单的事情就有无数种实现方法。不过既然用nodejs，就尽量按照它的标准来走。 Cheater这个类需要触发以及监听事件，所以需要继承EventEmitter这个类。 1 2 3 4 5 6 7 8 var events = require&#40;'events'&#41;; &#160; var Cheater = function &#40;&#41; &#123; events.EventEmitter.call&#40;this&#41;; ... &#125;; &#160; require&#40;'sys'&#41;.inherits&#40;Cheater, events.EventEmitter&#41;; 代码倒是简单得很，不过背后还是做了很多事情的。这里写得很详细，就不废话了：http://blog.nodejitsu.com/using-sys-inherits-in-node-js。 &#160; 更新：sys这个module在nodejs 0.3之后的版本改成了util，为了保持兼容，sys仍可以用，但是不推荐。]]></description>
			<content:encoded><![CDATA[<p>javascript的对象模型诡异得很，我等半脑残几乎无法领会其精要，仅继承这件简单的事情就有无数种实现方法。不过既然用nodejs，就尽量按照它的标准来走。</p>
<p>Cheater这个类需要触发以及监听事件，所以需要继承EventEmitter这个类。</p>

<div class="wp_codebox"><table><tr id="p7226"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p722code6"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> events <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'events'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> Cheater <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    events.<span style="color: #660066;">EventEmitter</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ...
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sys'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">inherits</span><span style="color: #009900;">&#40;</span>Cheater<span style="color: #339933;">,</span> events.<span style="color: #660066;">EventEmitter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>代码倒是简单得很，不过背后还是做了很多事情的。这里写得很详细，就不废话了：<a title="nodejs inherit" href="http://blog.nodejitsu.com/using-sys-inherits-in-node-js" target="_blank">http://blog.nodejitsu.com/using-sys-inherits-in-node-js</a>。</p>
<p>&nbsp;</p>
<p>更新：sys这个module在nodejs 0.3之后的版本改成了util，为了保持兼容，sys仍可以用，但是不推荐。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/722/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>对于cheater-node的总结——异步篇</title>
		<link>http://honnix.com/blog/archives/713</link>
		<comments>http://honnix.com/blog/archives/713#comments</comments>
		<pubDate>Sun, 24 Apr 2011 07:46:01 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[async]]></category>
		<category><![CDATA[cheater]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[异步]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=713</guid>
		<description><![CDATA[之前也说了，开始玩儿nodejs，然后拿cheater下手。做得差不多了，总结一下。会有几篇单独的文章，省得看起来乱。 nodejs的基本思想就是异步，这对传统编程模型是彻底的颠覆。刚开始很不习惯，死得很惨，程序怎么都不按我想的跑。一个简单的例子就是http的request，调用不会等待response而是立即返回，然后通过回调函数处理response。 对于这种异步的模型，通常有两种办法，一是回调函数＋事件传递，二是直接用回调函数处理所有事情。简单说明一下，第一种在回调函数中不做什么事情，只是简单地发出一个事件，由事件的listener处理： 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 var cheater = function &#40;&#41; &#123; this.on&#40;'login', function &#40;retryTimes&#41; &#123; this.once&#40;'login_ok', function &#40;&#41; &#123; // note that &#34;once&#34; is used, not &#34;on&#34; this.emit&#40;'heartbeat', 0, 0&#41;; &#125;&#41;; &#160; this.once&#40;'login_nok', function &#40;&#41; &#123; var that&#8230;]]></description>
			<content:encoded><![CDATA[<p>之前也说了，开始玩儿nodejs，然后拿cheater下手。做得差不多了，总结一下。会有几篇单独的文章，省得看起来乱。</p>
<p>nodejs的基本思想就是异步，这对传统编程模型是彻底的颠覆。刚开始很不习惯，死得很惨，程序怎么都不按我想的跑。一个简单的例子就是http的request，调用不会等待response而是立即返回，然后通过回调函数处理response。</p>
<p><span id="more-713"></span></p>
<p>对于这种异步的模型，通常有两种办法，一是回调函数＋事件传递，二是直接用回调函数处理所有事情。简单说明一下，第一种在回调函数中不做什么事情，只是简单地发出一个事件，由事件的listener处理：</p>

<div class="wp_codebox"><table><tr id="p71310"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p713code10"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> cheater <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'login'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>retryTimes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">once</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'login_ok'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// note that &quot;once&quot; is used, not &quot;on&quot;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">emit</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'heartbeat'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">once</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'login_nok'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> that <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>retryTimes <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> CheaterConfig.<span style="color: #660066;">loginMaxRetryTimes</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                timeoutId <span style="color: #339933;">=</span> setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    that.<span style="color: #660066;">emit</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'login'</span><span style="color: #339933;">,</span> retryTimes <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> CheaterConfig.<span style="color: #660066;">loginRetryInterval</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        client.<span style="color: #660066;">login</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// send out http request, later login_ok or login_nok event will be emitted</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>这里没有给client.login()注册任何回调函数，client.login()自己在调用https.request时注册了一个回调函数，这个函数仅仅用来发事件：</p>

<div class="wp_codebox"><table><tr id="p71311"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code" id="p713code11"><pre class="javascript" style="font-family:monospace;">CheaterClient.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">login</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> fs <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fs'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        cert<span style="color: #339933;">:</span> fs.<span style="color: #660066;">readFileSync</span><span style="color: #009900;">&#40;</span>CheaterConfig.<span style="color: #660066;">cert</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        host<span style="color: #339933;">:</span> CheaterConfig.<span style="color: #660066;">loginHost</span><span style="color: #339933;">,</span>
        port<span style="color: #339933;">:</span> CheaterConfig.<span style="color: #660066;">loginPort</span><span style="color: #339933;">,</span>
        path<span style="color: #339933;">:</span> CheaterConfig.<span style="color: #660066;">loginUrl</span><span style="color: #339933;">,</span>
        method<span style="color: #339933;">:</span> <span style="color: #3366CC;">'POST'</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> request <span style="color: #339933;">=</span> https.<span style="color: #660066;">request</span><span style="color: #009900;">&#40;</span>options<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        response.<span style="color: #660066;">setEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ascii'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        response.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>data <span style="color: #000066; font-weight: bold;">is</span> valid<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">cheater</span>.<span style="color: #660066;">emit</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'login_ok'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// everything is fine</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">cheater</span>.<span style="color: #660066;">emit</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'login_nok'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// oops, bad things just happened</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    request.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>CheaterConfig.<span style="color: #660066;">loginInfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    request.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>值得注意的是在注册login_ok和login_nok的时候并不是使用“on”，而是用了“once”，原因就在于为了不把那个retryTimes传来传去，这里用了closure，如果用“on”的话，每次login事件被捕获的时候都会注册login_ok和login_nok的listener，然后就……杯具了。</p>
<p>对于第二种方案就不多说了，比较容易理解，但是个人感觉代码会比较混乱，所以我没有使用。</p>
<p>还有一点值得一提，也是折磨我许久。看下面这段代码：</p>

<div class="wp_codebox"><table><tr id="p71312"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p713code12"><pre class="javascript" style="font-family:monospace;">Cheater.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">heartbeat</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>livingTime<span style="color: #339933;">,</span> failedTimes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>livingTime <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #CC0000;">3600</span> <span style="color: #339933;">/</span> CheaterConfig.<span style="color: #660066;">heartbeatInterval</span> <span style="color: #339933;">*</span> CheaterConfig.<span style="color: #660066;">hoursToLive</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">heartbeat</span><span style="color: #009900;">&#40;</span>livingTime <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> CheaterConfig.<span style="color: #660066;">heartbeatInterval</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>先梳理一下这段代码都干了什么。login之后开始做heartbeat，隔一段时间一次，然后过了几个小时之后logout。看起来很美好，heartbeat递归调用，直到某次调用返回之后doSomething()。</p>
<p>但是，杯具就发生了，doSomething被提前调用了！我们来看看为什么。</p>
<p>下面是函数调用桟（比较土，凑合着看吧）：</p>
<pre>cheater.heartbeat(0, 0);
--&gt; setTimeout(function, interval);
    --&gt; heartbeat(1, 0);
        --&gt; setTimeout(function, interval); // 这里异步出现了，线程在这一点上留下一个continuation，然后这次递归调用结束了！
    --&gt; doSomething(); // 并不像我们所想的，doSomething()被提前调用了，在逻辑上的递归还没有结束之前！</pre>
<p>这个问题害得我调了很久，看来惯性思维确实很难改变。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/713/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>npm安装东西时候的困惑</title>
		<link>http://honnix.com/blog/archives/711</link>
		<comments>http://honnix.com/blog/archives/711#comments</comments>
		<pubDate>Wed, 20 Apr 2011 15:49:10 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[global]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[local]]></category>
		<category><![CDATA[npm]]></category>
		<category><![CDATA[安装]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=711</guid>
		<description><![CDATA[不知道npm是什么的同学，自己google，提示一下，跟node相关。 npm安装分为两种，local和global，有什么区别这里说得已经很详细了：http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/，我也就不再废话了。 只是有些东西还要解释一下。 刚装好npm的时候，我惊异地发现它所有的安装目录全是777的权限，表示不理解（而且terminal里面看过去，非常不好看），手工改成755。这样带来的问题是如果做global安装的话，普通用户则无法安装；ok没问题，我可以sudo来搞，但是sudo之后新的问题又来了，~/.npm是做cache用的，sudo安装的话这里面东西可都属于root了。这个设计比较土，理想的方案是local安装，~/.npm作为cache，global安装，cache在npm自己的安装路径下。 还有local这种安装方式看起来隔离性挺好，但是对版本管理工具可不怎么友好，不知道有没有办法。]]></description>
			<content:encoded><![CDATA[<p>不知道npm是什么的同学，自己google，提示一下，跟node相关。</p>
<p>npm安装分为两种，local和global，有什么区别这里说得已经很详细了：<a href="http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/" target="_blank">http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/</a>，我也就不再废话了。</p>
<p>只是有些东西还要解释一下。</p>
<p>刚装好npm的时候，我惊异地发现它所有的安装目录全是777的权限，表示不理解（而且terminal里面看过去，非常不好看），手工改成755。这样带来的问题是如果做global安装的话，普通用户则无法安装；ok没问题，我可以sudo来搞，但是sudo之后新的问题又来了，~/.npm是做cache用的，sudo安装的话这里面东西可都属于root了。这个设计比较土，理想的方案是local安装，~/.npm作为cache，global安装，cache在npm自己的安装路径下。</p>
<p>还有local这种安装方式看起来隔离性挺好，但是对版本管理工具可不怎么友好，不知道有没有办法。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/711/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>其实根本没有object，所有的东西都是function</title>
		<link>http://honnix.com/blog/archives/709</link>
		<comments>http://honnix.com/blog/archives/709#comments</comments>
		<pubDate>Sun, 17 Apr 2011 08:32:03 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[OO]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=709</guid>
		<description><![CDATA[我是意思是js里压根儿就没有object，全是浮云；其实根本就是function。 越看越糊涂，这语言设计得真是够可以的。都说是OO，其实就是个屁，所有东西都是用function模拟的。没有任何东西能跟传统意义上的OO对应起来，然后就说这门语言比较深奥，受过传统程序设计教育的人不容易理解。操蛋也不能操成这样吧？如果你说我就是不支持OO，OO算个屁，那也好很多。 只是发发牢骚，不能这么让人忽悠了，但是该学还是要好好学，既然它这么设计了，就咬牙跟进吧。]]></description>
			<content:encoded><![CDATA[<p>我是意思是js里压根儿就没有object，全是浮云；其实根本就是function。</p>
<p>越看越糊涂，这语言设计得真是够可以的。都说是OO，其实就是个屁，所有东西都是用function模拟的。没有任何东西能跟传统意义上的OO对应起来，然后就说这门语言比较深奥，受过传统程序设计教育的人不容易理解。操蛋也不能操成这样吧？如果你说我就是不支持OO，OO算个屁，那也好很多。</p>
<p>只是发发牢骚，不能这么让人忽悠了，但是该学还是要好好学，既然它这么设计了，就咬牙跟进吧。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/709/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>果然js给我上的第一课就是this</title>
		<link>http://honnix.com/blog/archives/705</link>
		<comments>http://honnix.com/blog/archives/705#comments</comments>
		<pubDate>Fri, 15 Apr 2011 15:29:31 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[that]]></category>
		<category><![CDATA[this]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=705</guid>
		<description><![CDATA[this在js里设计得还真是够复杂，或者确切地说根本就是设计错了（这不是我说的，是写那本the good parts的老大说的）。 一个例子： 1 2 3 4 5 6 7 myObject.double = function &#40;&#41; &#123; var helper = function &#40;&#41; &#123; this.value = add&#40;this.value, this.value&#41;; &#125;; &#160; helper&#40;&#41;; &#125;; 这个方法要做的事情很简单，就是把自己的value属性double一下。一切看起来都很完美，直到真正跑起来，你才会发现报错说value没定义。 原因在于那个内部匿名函数里的this并不是myObject，而是这个函数本身！诡异得很。 解决起来倒是简单： 1 2 3 4 5 6 7 8 9 myObject.double = function &#40;&#41; &#123; var that = this; &#160; var helper =&#8230;]]></description>
			<content:encoded><![CDATA[<p>this在js里设计得还真是够复杂，或者确切地说根本就是设计错了（这不是我说的，是写那本the good parts的老大说的）。</p>
<p>一个例子：</p>

<div class="wp_codebox"><table><tr id="p70515"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p705code15"><pre class="javascript" style="font-family:monospace;">myObject.<span style="color: #660066;">double</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> helper <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> add<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    helper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>这个方法要做的事情很简单，就是把自己的value属性double一下。一切看起来都很完美，直到真正跑起来，你才会发现报错说value没定义。</p>
<p>原因在于那个内部匿名函数里的this并不是myObject，而是这个函数本身！诡异得很。</p>
<p>解决起来倒是简单：</p>

<div class="wp_codebox"><table><tr id="p70516"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p705code16"><pre class="javascript" style="font-family:monospace;">myObject.<span style="color: #660066;">double</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> that <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> helper <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        that.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> add<span style="color: #009900;">&#40;</span>that.<span style="color: #660066;">value</span><span style="color: #339933;">,</span> that.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    helper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>习惯上大家都用that。怪不得之前总是看到that这that那的，还以为是js的保留字。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/705/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>关于node.js的第一贴</title>
		<link>http://honnix.com/blog/archives/693</link>
		<comments>http://honnix.com/blog/archives/693#comments</comments>
		<pubDate>Sat, 09 Apr 2011 17:02:42 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[node.js]]></category>
		<category><![CDATA[socket]]></category>
		<category><![CDATA[异步]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=693</guid>
		<description><![CDATA[node.js是一个server端js的运行时。 想研究一下，就拿cheater来练手。（注：cheater是在公司里对付cc zone的实现自动登录的小工具） 首先想到的是用来做管理、监控的socket端口。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 var net = require&#40;'net'&#41;; var server = net.createServer&#40;function &#40;client&#41; &#123; // 有client连上来的时候调用此回调函数 client.setEncoding&#40;'ascii'&#41;; // 设置编码，否则下面的data默认使用Buffer client.write&#40;'&#62; '&#41;; client.on&#40;'data', function &#40;data&#41; &#123; // client发数据过来，触发'data'事件，然后调用此回调函数 switch &#40;data.trim&#40;&#41;&#41; &#123; case&#8230;]]></description>
			<content:encoded><![CDATA[<p><a title="node.js" href="http://nodejs.org/" target="_blank">node.js</a>是一个server端js的运行时。</p>
<p>想研究一下，就拿cheater来练手。（注：cheater是在公司里对付cc zone的实现自动登录的小工具）</p>
<p>首先想到的是用来做管理、监控的socket端口。</p>

<div class="wp_codebox"><table><tr id="p69318"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code" id="p693code18"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> net <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'net'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> server <span style="color: #339933;">=</span> net.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>client<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// 有client连上来的时候调用此回调函数</span>
  client.<span style="color: #660066;">setEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ascii'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 设置编码，否则下面的data默认使用Buffer</span>
  client.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&gt; '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  client.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// client发数据过来，触发'data'事件，然后调用此回调函数</span>
    <span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'status'</span><span style="color: #339933;">:</span>
        client.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'heartbeating...<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        client.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&gt; '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'quit'</span><span style="color: #339933;">:</span>
        client.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 关掉client</span>
        <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'shutdown'</span><span style="color: #339933;">:</span>
        client.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        server.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 关掉server</span>
        <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">default</span><span style="color: #339933;">:</span>
        client.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&gt; '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
server.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">44050</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'localhost'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>值得注意的是，这里只有一个线程，所有异步的事情都靠事件驱动。这也就是node.js一个最核心的思想：用单个线程充分压榨CPU资源，能异步的全都异步了。</p>
<p>刚开始玩儿，哪里不对的，请多指正。顺便帮<a title="cnodejs" href="http://cnodejs.org/"  target="_blank">cnodejs</a>做个广告。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/693/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>如何在Lift里用javascript的confirm</title>
		<link>http://honnix.com/blog/archives/566</link>
		<comments>http://honnix.com/blog/archives/566#comments</comments>
		<pubDate>Thu, 13 May 2010 15:30:48 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Lift]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[anchor]]></category>
		<category><![CDATA[confirm]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=566</guid>
		<description><![CDATA[使用Lift的SHtml.a()设计ajax调用的时候，老版本的Lift不支持给onclick加入用户自己定义的javascript方法，新版本的有了。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 /** * Create an anchor tag around a body which will do an AJAX call and invoke the function * * @param jsFunc -- the user function that will be executed. This function will receive as last parameter * the&#8230;]]></description>
			<content:encoded><![CDATA[<p>使用Lift的SHtml.a()设计ajax调用的时候，老版本的Lift不支持给onclick加入用户自己定义的javascript方法，新版本的有了。</p>

<div class="wp_codebox"><table><tr id="p56621"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p566code21"><pre class="scala" style="font-family:monospace;"><span style="color: #00ff00; font-style: italic;">/**
   * Create an anchor tag around a body which will do an AJAX call and invoke the function
   *
   * @param jsFunc -- the user function that will be executed. This function will receive as last parameter
   *                  the function that will actually do the ajax call. Hence the user function can decide when
   * 				  to make the ajax request.
   * @param func - the function to invoke when the link is clicked
   * @param body - the NodeSeq to wrap in the anchor tag
   * @param attrs - the anchor node attributes
   */</span>
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> a<span style="color: #F78811;">&#40;</span>jsFunc<span style="color: #000080;">:</span> Call, func<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;;</span> JsCmd, body<span style="color: #000080;">:</span> NodeSeq, attrs<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>String, String<span style="color: #F78811;">&#41;</span><span style="color: #000080;">*</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Elem <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    attrs.<span style="color: #000000;">foldLeft</span><span style="color: #F78811;">&#40;</span>fmapFunc<span style="color: #F78811;">&#40;</span>contextFuncBuilder<span style="color: #F78811;">&#40;</span>func<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>name <span style="color: #000080;">=&gt;;</span>
            <span style="color: #000080;">&lt;</span>a onclick<span style="color: #000080;">=</span><span style="color: #6666FF;">&quot;{deferCall(Str(name&quot;</span> href<span style="color: #000080;">=</span><span style="color: #6666FF;">&quot;javascript://&quot;</span><span style="color: #000080;">&gt;</span><span style="color: #F78811;">&#123;</span>body<span style="color: #F78811;">&#125;</span><span style="color: #000080;">&lt;</span>/a<span style="color: #000080;">&gt;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span> <span style="color: #000080;">%</span> <span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>对于jsFunc可以这样理解：<br />
用户定义了一个方法：</p>

<div class="wp_codebox"><table><tr id="p56622"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p566code22"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #000066; font-weight: bold;">delete</span><span style="color: #009900;">&#40;</span>toDelete<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">confirm</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Delete?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    toDelete<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>那么jsFunc可以这样定义：Call(&#8220;delete&#8221;)，Lift会把它生成的ajax方法作为最后一个参数传递给delete方法。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/566/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>感觉Safari比Firefox快多了</title>
		<link>http://honnix.com/blog/archives/432</link>
		<comments>http://honnix.com/blog/archives/432#comments</comments>
		<pubDate>Thu, 10 Sep 2009 23:45:22 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Safari]]></category>
		<category><![CDATA[速度]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=432</guid>
		<description><![CDATA[看网上的测试都说JS的处理Firefox要快，但是就我的感觉来说，Safari要快不少，用Google Reader的时候特别明显，FF有点卡，Safari完全没有问题。 FF的冷启动实在是太慢了，已经不能接受了。Safari快上很多倍。 所以Mac下准备投靠Safari了，唯一不爽的是没有Delicious的插件可以用。]]></description>
			<content:encoded><![CDATA[<p>看网上的测试都说JS的处理Firefox要快，但是就我的感觉来说，Safari要快不少，用Google Reader的时候特别明显，FF有点卡，Safari完全没有问题。</p>
<p>FF的冷启动实在是太慢了，已经不能接受了。Safari快上很多倍。</p>
<p>所以Mac下准备投靠Safari了，唯一不爽的是没有Delicious的插件可以用。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/432/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

