<?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>10V</title>
	<atom:link href="http://www.smartwei.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.smartwei.com</link>
	<description>干一行，爱一行</description>
	<lastBuildDate>Sun, 26 Feb 2012 04:47:19 +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>nodeType值对应的节点类型</title>
		<link>http://www.smartwei.com/nodetype-value.html</link>
		<comments>http://www.smartwei.com/nodetype-value.html#comments</comments>
		<pubDate>Sun, 26 Feb 2012 04:36:19 +0000</pubDate>
		<dc:creator>10V</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[nodeType]]></category>

		<guid isPermaLink="false">http://www.smartwei.com/?p=386</guid>
		<description><![CDATA[nodeType值对应的节点类型，对于不支持nodeType值的IE浏览器，提供一种解决办法]]></description>
			<content:encoded><![CDATA[<p>elementNode.nodeType值对应的类型：</p>
<p>1  =&gt;  Element | 元素</p>
<p>2  =&gt;  Attribute | 属性</p>
<p>3  =&gt;  Text | 文本</p>
<p>4  =&gt;  CDATA Section | CDATA 片段</p>
<p>5  =&gt;  Entity Reference | 实体参数</p>
<p>6  =&gt;  Entity | 实体</p>
<p>7  =&gt;  Processing Instrucion | 处理指令</p>
<p>8  =&gt;  Comment | 注释</p>
<p>9  =&gt;  Document | 文档</p>
<p>10 =&gt; Document Type | 文档类型</p>
<p>11 =&gt;  Document Fragment | 文档片断</p>
<p>12 =&gt;  Notation | 符号</p>
<p>目前IE对这个属性支持的还不够好，可以加入下面的代码片段来保证节点属性在各个浏览器中都能得到很好的支持。</p>
<pre class="brush: jscript; title: ; notranslate">
if (!window.Node){
  var Node =
      {
        ELEMENT_NODE                :  1,
        ATTRIBUTE_NODE              :  2,
        TEXT_NODE                   :  3,
        CDATA_SECTION_NODE          :  4,
        ENTITY_REFERENCE_NODE       :  5,
        ENTITY_NODE                 :  6,
        PROCESSING_INSTRUCTION_NODE :  7,
        COMMENT_NODE                :  8,
        DOCUMENT_NODE               :  9,
        DOCUMENT_TYPE_NODE          : 10,
        DOCUMENT_FRAGMENT_NODE      : 11,
        NOTATION_NODE               : 12
      };
}
</pre>
<p>使用方法如下：</p>
<pre class="brush: jscript; title: ; notranslate">
/* Outputs the contents of all comment nodes that are descendants of the
 * specified node. The parameter is:
 *
 * node - the node whose descendants should be searched
 */
function outputComments(node){

  // initialise the child node
  var child = node.firstChild;

  // loop while the child node exists
  while (child){

    // determine the type of the node
    switch (child.nodeType){

      // if the node is an element node, recurse into it
      case Node.ELEMENT_NODE : outputComments(child); break;

      // if the node is a comment node, output its value
      case Node.COMMENT_NODE : alert(child.nodeValue); break;

    }

    // move to the next child node
    child = child.nextSibling;

  }

}

// output the content of all comment nodes in the document
outputComments(document.body);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.smartwei.com/nodetype-value.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>获取DOM元素的样式</title>
		<link>http://www.smartwei.com/get-dom-element-style.html</link>
		<comments>http://www.smartwei.com/get-dom-element-style.html#comments</comments>
		<pubDate>Sun, 26 Feb 2012 04:21:56 +0000</pubDate>
		<dc:creator>10V</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[Dom 样式]]></category>

		<guid isPermaLink="false">http://www.smartwei.com/?p=381</guid>
		<description><![CDATA[js获取DOM元素样式，IE使用elem.currentStyle[name]，FF使用document.defaultView.getComputedStyle[elem,null].getPropertyValue(name);]]></description>
			<content:encoded><![CDATA[<p>因为写js经常需要用到访问样式，我们常用的做法是通过<strong>DOM.style.XXX</strong>来读写样式信息的。可是<strong>DOM.style</strong>这种写法只能访问，这样在标签里内置的样式，如果样式写在，或者.css文件里，那么就办法读到样式了。</p>
<p>其实呢，还有别的方法可以读到这些样式信息，方法有两种，一种是通过<strong>document.styleSheets</strong>对象，另一种是通过“最终样式”对象。其中 IE中这个对象叫做<strong>currentStyle</strong>，FF中这个对象叫做<strong>document.defaultView</strong>。</p>
<p>下面是一个获取元素样式的方法：</p>
<pre class="brush: jscript; title: ; notranslate">
function getStyle( elem, name ) {
    var elem= typeof elem == &quot;string&quot; ? document.getElementById(elem) : elem;
    //如果该属性存在于style[]中，则它最近被设置过(且就是当前的)
    if (elem.style[name]) return elem.style[name];
    //否则，尝试IE的方式
    else if (elem.currentStyle) return elem.currentStyle[name];
    //或者W3C的方法，如果存在的话
    else if (document.defaultView &amp;amp;&amp;amp; document.defaultView.getComputedStyle) {
        //它使用传统的&quot;text-Align&quot;风格的规则书写方式，而不是&quot;textAlign&quot;
        name = name.replace(/([A-Z])/g,&quot;-$1&quot;);
        name = name.toLowerCase();
        //获取style对象并取得属性的值(如果存在的话)
        var s = document.defaultView.getComputedStyle(elem,&quot;&quot;);
        return s &amp;amp;&amp;amp; s.getPropertyValue(name);
    //否则，就是在使用其它的浏览器
    } else return null;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.smartwei.com/get-dom-element-style.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>一些PHP Coding Tips (Laruence文章中自己平时没有注意到的地方)</title>
		<link>http://www.smartwei.com/php-coding-tips-note.html</link>
		<comments>http://www.smartwei.com/php-coding-tips-note.html#comments</comments>
		<pubDate>Wed, 06 Apr 2011 09:44:23 +0000</pubDate>
		<dc:creator>10V</dc:creator>
				<category><![CDATA[php知识]]></category>
		<category><![CDATA[coding Tips]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.smartwei.com/?p=314</guid>
		<description><![CDATA[以下是一些PHP Coding Tips. 当然, 这些Tips并不一定仅仅局限于PHP.]]></description>
			<content:encoded><![CDATA[<p>读了<a href="http://www.laruence.com">Laruence</a>的“<a href="http://www.laruence.com/2011/03/24/858.html">一些PHP Coding Tips</a>”这篇文章之后，发现自己有许多细节平时编程都没有注意到：</p>
<p><b>1. 使用NULL === 来代替is_null:</b><br />
is_null和 NULL === 完全是一样的效果, 但是却节省了一次函数调用.</p>
<p><b>2. 使用===尽量不用==:</b><br />
PHP有俩组相等比较运算符===/!==和==/!=, ==/!=会有隐式类型转换,而===/!==会严格比较俩个操作时是否类型相同并且值相等.<br />
我们应该尽量使用===而不是==, 除了因为转换规则比较难记以外, 还有一点就是如果使用===, 对于日后的维护或者阅读你代码的人也会很舒服:”在这个时刻, 这一行语句, 这个变量就是这个类型的!”.</p>
<p><b>3. 少用/不用 continue:</b><br />
continue是回到循环的头部, 而循环结束本来就是回到循环的头部, 所以通过适当的构造, 我们完全可以避免使用这条语句, 使得效率得到改善.</p>
<p><b style="color:#C00;">4. 警惕switch/in_array等的松比较(loose comparision):</b><br />
switch和in_array都是采用松比较, 所以在要比较的变量之间类型不一样的时候, 很容易出错:</p>
<pre class="brush: php; title: ; notranslate">
switch ($name) {
   case &quot;laruence&quot;:
		...
		break;
   case &quot;eve&quot;:
		...
		break;
}
</pre>
<p>对于上面的switch, 如果$name是数字0, 那么它会满足任何一条case. 同理在in_array中也是.<br />
解决的办法就是, 在switch之前, 把变量类型转换成你所期望的类型.</p>
<pre class="brush: php; title: ; notranslate">
switch (strval($name)) {
	case &quot;laruence&quot;:
	   ...
			break;
	case &quot;eve&quot;:
	   ...
			break;
}
</pre>
<p>而, in_array提供了第三个可选的参数, 通过这个参数可以改变默认的比较方式.</p>
<p><b>4. switch不仅仅只用来判别变量:</b><br />
比如, 对于如下的一段代码:</p>
<pre class="brush: php; title: ; notranslate">
if($a) {
} else if ($b) {
} else if ($c || $d) {
}
</pre>
<p>可以简单的改写为:</p>
<pre class="brush: php; title: ; notranslate">
switch (TRUE) {
	case $a:
	   break;
	case $b:
	   break;
	case $c:
	case $d:
	   break;
}
</pre>
<p>是不是看起来更清晰呢?</p>
<p><b>5. 变量先定义后使用:</b><br />
使用一个未定义的变量, 比使用一个定义好的变量要慢8倍以上!<br />
可以相像, PHP引擎会首先按照正常的逻辑来获取这个变量, 然而这个变量不存在, 所以PHP引擎需要抛出一个NOTICE, 并且进入一段使用未定义变量时应该走的逻辑, 然后返回一个新的变量.<br />
另外, 阅读代码的角度讲, 当你使用一个未定义的变量时, 会让阅读你代码的人困惑:”这个变量在那里初始化的, 和之前的代码有关系么? 和include进来的文件有关系么?”<br />
最后, 从规范编程的角度来讲, 你也需要这样做.</p>
<p><b>6. 不用第三变量交换俩个变量的值:</b></p>
<pre class="brush: php; title: ; notranslate">
list($a, $b) = array($b, $a),
</pre>
<p>但其实还是有匿名临时变量的产生, 对于整数来说, 采用互逆的运算来做, 还是比较靠谱:</p>
<pre class="brush: php; title: ; notranslate">
$a = $a + $b;
$b = $a - $b;
$a = $a - $b;
</pre>
<p>不过, 还是用异或比较好, 因为+ – * /容易产生精度丢失或者溢出.</p>
<p><b>7. floor == 俩次非运算(此条由skiyo提供)</b></p>
<pre class="brush: php; title: ; notranslate">
echo ~~4.9;
echo floor(4.9);
</pre>
<p>用俩次非运算的速度基本上是floor的3倍, 不过有一点, 对于大数来说, 可能会发生溢出:</p>
<pre class="brush: php; title: ; notranslate">
echo ~~99999999999999.99; //276447231
echo floor(99999999999999.99); //99999999999999
</pre>
<p><b>8. do{}while(0)妙用(此条由Qianfeng提供)</b><br />
我们知道do{}while(0)在c/c++中, 有很多妙用, 比如消除goto, 宏定义代码块.<br />
所以, PHP中同理, 也可以用do{}while(0)来做一些巧妙的应用</p>
<pre class="brush: php; title: ; notranslate">
do{
   if(true) {
		break;
   }
   if(true) {
		break;
   }
} while(false);
//好过
if(true) {
} else if(true) {
} else {
}
</pre>
<p><b>9. 尽量少用@错误抑制符</b><br />
如下代码:</p>
<pre class="brush: php; title: ; notranslate">
@func();
</pre>
<p>就相当于(参见深入理解PHP原理之错误抑制与内嵌HTML):</p>
<pre class="brush: php; title: ; notranslate">
$report = error_reporting(0);
func();
error_reporting($report);
</pre>
<p>另外错误抑制符号, 可能会造成一些问题, 参看(PHP错误抑制符(@)导致引用传参失败的Bug);<br />
最后,错误抑制符在发生错误调试的时候也可能会带来麻烦.</p>
<p><b>10. 尽量避免使用递归(此条来自lazyboy)</b><br />
递归性能堪忧, 而大部分的递归都是尾递归, 都是可以消除的.</p>
<pre class="brush: php; title: ; notranslate">
function f($n) {
	if ($n = 0) return 1;
	return $n * f($n - 1);
}
 //变为:
$result = 1;
for ($y = 1; $y &lt; $n + 1; $y++ ) {
	$result *= $y;
}
</pre>
<p><b>11. 使用$_SERVER['REQUEST_TIME']代替time()</b></b><br />
time()会引来一次函数调用, 而如果对时间的精确值要求不高, 可以使用$_SERVER['REQUEST_TIME']代替, 快很多.</p>
<p><b>12. 尽量避免使用正则(此条来自pangyontao)</b><br />
正则耗时, 尽量避免, 而采用直接的字符串处理函数代替, 如:</p>
<pre class="brush: php; title: ; notranslate">
if (preg_match(&quot;!^foo_!i&quot;, &quot;FoO_&quot;)) { }
// 替换为:
if (!strncasecmp(&quot;foo_&quot;, &quot;FoO_&quot;, 4)) { }
if (preg_match(&quot;![a8f9]!&quot;, &quot;sometext&quot;)) { }
// 替换为:
if (strpbrk(&quot;a8f9&quot;, &quot;sometext&quot;)) { }
if (preg_match(&quot;!string!i&quot;, &quot;text&quot;)) {}
// 替换为:
if (stripos(&quot;text&quot;, &quot;string&quot;) !== false) {}
</pre>
<p>等等.</p>
<p><b>13. 用FALSE表示错误, 用NULL表示不存在.</b><br />
对于操作类的函数, 失败返回FALSE, 表示”操作失败了”, 而对于查询类的函数, 如果找不到想要的值, 则应该返回NULL, 表示”找不到”.</p>
<p>作者: <a href="http://www.smartwei.com/php-coding-tips-note.html">石巍</a><br />
原载: <a href="http://www.laruence.com/2011/03/24/858.html">风雪之隅->一些PHP Coding Tips</a><br />
版权所有，转载时必须以超链接形式注明作者和原始出处及本声明。</p>
<p>本文链接: <a href="http://www.smartwei.com/php-coding-tips-note.html">http://www.smartwei.com/javascript-url-redirect.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.smartwei.com/php-coding-tips-note.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Firebug &amp; Chrome Console 控制台使用指南</title>
		<link>http://www.smartwei.com/firebug-chrome-console-guid.html</link>
		<comments>http://www.smartwei.com/firebug-chrome-console-guid.html#comments</comments>
		<pubDate>Wed, 23 Mar 2011 16:48:18 +0000</pubDate>
		<dc:creator>10V</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[常用工具]]></category>
		<category><![CDATA[firebug]]></category>

		<guid isPermaLink="false">http://www.smartwei.com/?p=312</guid>
		<description><![CDATA[本文介绍了如何使用firebug的console（控制台）来调试javascript]]></description>
			<content:encoded><![CDATA[<div style="font: bold 18px Arial;color: #C00;margin-top:20px">Console API</div>
<p>当打开 firebug (也包括 Chrome 等浏览器的自带调试工具)，window 下面会注册一个叫做 console 的对象，它提供多种方法向控制台输出信息，供开发人员调试使用。下面是这些方法的一个简单介绍，适时地运用它们，对于提高开发效率很有帮助。</p>
<p><strong>console.log(object[, object, ...])</strong><br />
使用频率最高的一条语句：向控制台输出一条消息。支持 C 语言 printf 式的格式化输出。当然，也可以不使用格式化输出来达到同样的目的：</p>
<pre lang="javascript">
var animal='frog', count=10;
console.log("The %s jumped over %d tall buildings", animal, count);
console.log("The", animal, "jumped over", count, "tall buildings");
</pre>
<p><strong>console.debug(object[, object, ...])</strong><br />
向控制台输出一条信息，它包括一个指向该行代码位置的超链接。</p>
<p><strong>console.info(object[, object, ...])</strong><br />
向控制台输出一条信息，该信息包含一个表示“信息”的图标，和指向该行代码位置的超链接。</p>
<p><strong>console.warn(object[, object, ...])</strong><br />
同 info。区别是图标与样式不同。</p>
<p><strong>console.error(object[, object, ...])</strong><br />
同 info。区别是图标与样式不同。error 实际上和 throw new Error() 产生的效果相同，使用该语句时会向浏览器抛出一个 js 异常。</p>
<p><strong>console.assert(expression[, object, ...])</strong><br />
断言，测试一条表达式是否为真，不为真时将抛出异常（断言失败）。</p>
<p><strong>console.dir(object)</strong><br />
输出一个对象的全部属性（输出结果类似于 DOM 面板中的样式）。</p>
<p><strong>console.dirxml(node)</strong><br />
输出一个 HTML 或者 XML 元素的结构树，点击结构树上面的节点进入到 HTML 面板。</p>
<p><strong>console.trace()</strong><br />
输出 Javascript 执行时的堆栈追踪。</p>
<p><strong>console.group(object[, object, ...])</strong><br />
输出消息的同时打开一个嵌套块，用以缩进输出的内容。调用 console.groupEnd() 用以结束这个块的输出。</p>
<p><strong>console.groupCollapsed()</strong><br />
同 console.group(); 区别在于嵌套块默认是收起的。</p>
<p><strong>console.time(name)</strong><br />
计时器，当调用 console.timeEnd(name);并传递相同的 name 为参数时，计时停止，并输出执行两条语句之间代码所消耗的时间（毫秒）。</p>
<p><strong>console.profile([title])</strong><br />
与 profileEnd() 结合使用，用来做性能测试，与 console 面板上 profile 按钮的功能完全相同。</p>
<p><strong>console.count([title])</strong><br />
输出该行代码被执行的次数，参数 title 将在输出时作为输出结果的前缀使用。</p>
<p><strong>console.clear()</strong><br />
清空控制台</p>
<div style="font: bold 18px Arial;color: #C00;margin-top:20px">命令行</div>
<p>控制台的输出面板右边，是控制台的输入面板（Chrome 调试工具对应为下方），在这里除了可以运行常规的 javascript 代码，还内置了相当数量的命令行可以辅助我们的调试工作，下面是一些常用命令行的简单介绍。</p>
<p><strong>$(id)</strong><br />
返回一个给定 id 的元素。</p>
<p><strong>$$(selector)</strong><br />
返回给定的 css 选择器匹配到的一组元素。</p>
<p><strong>$x(xpath)</strong><br />
返回给定的 XPath 表达式匹配到的一组元素。</p>
<p><strong>$0</strong><br />
在 HTML 面板中选中的元素。</p>
<p><strong>$1</strong><br />
上一次在 HTML 面板中选中的元素。</p>
<p><strong>$n(index)</strong><br />
访问最近 5 个被选中过的元素，index 的范围： 0 &#8211; 4。</p>
<p><strong>dir(object)</strong><br />
同 console.dir(object)。</p>
<p><strong>dirxml(node)</strong><br />
同 console.dirxml(node)。</p>
<p><strong>clear()</strong><br />
同 console.clear()。</p>
<p><strong>inspect(object[, tabName])()</strong><br />
在合适的（或一个指定的） tab 中检视一个对象。</p>
<p><strong>keys(object)</strong><br />
返回一个对象的所有属性的键。</p>
<p><strong>values(object)</strong><br />
返回一个对象的所有属性的值。</p>
<p><strong>debug(fn)</strong><br />
在函数第一行添加一个断点，使用 undebug(fn) 移除断点。</p>
<p><strong>monitor(fn)</strong><br />
开启一个函数的调用日志，使用 unmonitor(fn) 关闭该功能。非常有用的一个命令，但是它似乎并没有很好地工作。</p>
<p><strong>monitorEvents(object[, types])</strong><br />
开启一个元素的某个事件（或所有事件）被触发时的日志记录。用例如下：</p>
<pre lang="javascript">
monitorEvents($0,['click'])
</pre>
<p>上面的命令行被执行后，将开启当前在 HTML 面板中被选中元素的 click 事件监控，一旦这个元素的 click 事件被触发，事件对象将会在控制台输出。如果不指定第二个参数，将对所有事件进行记录。</p>
<p><strong>profile([title])</strong><br />
同 console.profile([title])</p>
]]></content:encoded>
			<wfw:commentRss>http://www.smartwei.com/firebug-chrome-console-guid.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>《javascript精粹》读书笔记 – 数组</title>
		<link>http://www.smartwei.com/%e3%80%8ajavascript%e7%b2%be%e7%b2%b9%e3%80%8b%e8%af%bb%e4%b9%a6%e7%ac%94%e8%ae%b0-%e6%95%b0%e7%bb%84.html</link>
		<comments>http://www.smartwei.com/%e3%80%8ajavascript%e7%b2%be%e7%b2%b9%e3%80%8b%e8%af%bb%e4%b9%a6%e7%ac%94%e8%ae%b0-%e6%95%b0%e7%bb%84.html#comments</comments>
		<pubDate>Sun, 23 Jan 2011 15:27:24 +0000</pubDate>
		<dc:creator>10V</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[javascript精萃]]></category>

		<guid isPermaLink="false">http://www.smartwei.com/?p=309</guid>
		<description><![CDATA[记录《javascript精粹》中“数组”这一章节的知识点]]></description>
			<content:encoded><![CDATA[<p><strong>1. </strong>数组length属性的值是这个数组的最大整数属性名加上1.它不一定等于数组里属性的个数：</p>
<pre class="brush: jscript; title: ; notranslate">
var myArray = [];
alert(myArray.length); // 0

myArray[1000000] = true;
alert(myArray.length); //1000001
</pre>
<p><strong>2. </strong>在数组尾部添加一个新元素：</p>
<pre class="brush: jscript; title: ; notranslate">
var numbers = ['zero', 'one', 'two'];
numbers[numbers.length] = 'three';
numbers.push('four');
</pre>
<p><strong>3. </strong>删除数组元素。<br />
js中的数组实际上就是对象，所以delete运算符就可以用来从数组中移除元素：</p>
<pre class="brush: jscript; title: ; notranslate">
var numbers = ['one', 'two', 'three', 'four'];
delete numbers[2]; // ['one', 'two', undefined, 'four']
</pre>
<p>不幸的是，那样会在数组中遗留一个空洞。 通常可以使用splice方法来删除元素：</p>
<pre class="brush: jscript; title: ; notranslate">
var numbers = ['one', 'two', 'three', 'four'];
numbers.splice(2, 1); //['one', 'two', 'four']
</pre>
<p>这个对大型数组来说可能会效率不高。</p>
<p><strong>4. </strong>当属性名是小而连续的整数时，应该使用数组；否则使用对象。</p>
<p>5. 判断是否是数组：</p>
<pre class="brush: jscript; title: ; notranslate">
var is_array = function(value){
	return value &amp;&amp;
		typeof value === 'object' &amp;&amp;
		value.constructor === Array;
};
</pre>
<p>不幸的是，它在识别从不同的窗口(window)或帧(frame)里构造的数组时会失败。完整的测试方法如下：</p>
<pre class="brush: jscript; title: ; notranslate">
var is_array = function(value){
	return value &amp;&amp;
		typeof value === 'object' &amp;&amp;
		typeof value.length === 'number' &amp;&amp;
		typeof value.splice === 'function' &amp;&amp;
		!(value.propertyIsEnumberable('length'));
};
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.smartwei.com/%e3%80%8ajavascript%e7%b2%be%e7%b2%b9%e3%80%8b%e8%af%bb%e4%b9%a6%e7%ac%94%e8%ae%b0-%e6%95%b0%e7%bb%84.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>《javascript精粹》读书笔记 – 函数</title>
		<link>http://www.smartwei.com/javascript-good-part-note-function.html</link>
		<comments>http://www.smartwei.com/javascript-good-part-note-function.html#comments</comments>
		<pubDate>Fri, 14 Jan 2011 01:49:19 +0000</pubDate>
		<dc:creator>10V</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[javascript 闭包]]></category>
		<category><![CDATA[javascript精萃]]></category>

		<guid isPermaLink="false">http://www.smartwei.com/?p=305</guid>
		<description><![CDATA[记录《javascript精粹》中“函数”这一章节的知识点]]></description>
			<content:encoded><![CDATA[<p>1. 当实际参数的个数与形式参数的个数不匹配时，不会导致错误。<br />
如果实际参数值过多了，超出的参数值将被忽略；如果实际的参数值过少，缺失的值将被替换为undefined。</p>
<p>2. 在javascript中一共有四种调用模式： 方法调用模式、函数调用模式、构造器调用模式和apply调用模式。这些模式在如何初始化关键参数this上存在差异。<br />
a.方法调用模式：<br />
当一个函数被保存为对象的一个属性时，我们称它为一个方法。方法调用模式可以使用this去访问对象，所以它能够从对象中取值或修改对象。<br />
b.函数调用模式：<br />
当一个函数并非一个对象的属性时，它被当作一个函数来调用。当函数以此模式调用时，this被绑定到全局变量，这是语言设计上的一个错误。（正确的情况：当内部函数被调用时，this应该仍然被绑定到外部函数的this变量。）解决方法：如果该方法定义一个变量并给他赋值为this,那么内部函数就可以通过那个变量访问到this,按照约定，给那个变量命名为 that：</p>
<pre class="brush: jscript; title: ; notranslate">
	//给 myObject增加一个double方法
	myObject.double = function()
	{
		var that = this; //解决方法

		var helper = function()
		{
			that.value = add(that.value, that.value);
		}

		helper(); //以函数的形式调用helper。
	}

	//以方法的形式调用double
	myObject.double();
</pre>
<p>c.构造器调用模式：<br />
在一个函数前面带上new来调用，那么将创建一个隐藏连接到该函数的prototype成员的新对象，同时this将会被绑定到那个新对象上。</p>
<pre class="brush: jscript; title: ; notranslate">
	//创建一个名为Quo的构造函数。它构造一个带有status属性的对象
	var Quo = function(String)
	{
		this.status = String;
	}

	//给Quo的所有实例提供一个名为get_status的公共方法
	Quo.prototype.get_status = function()
	{
		return this.status;
	}

	//构造一个Quo实例
	var myQuo = new Quo(&quot;confused&quot;); //构造器模式调用
	document.writeln(myQuo.get_status());
</pre>
<p>按照约定，构造器函数保存在以大写格式命名的变量里。<br />
d.Apply调用模式:<br />
apply方法接受两个参数，第一个是将被绑定给this的值，第二个就是参数数组。它让我们构建一个参数数组并用其去调用函数。</p>
<pre class="brush: jscript; title: ; notranslate">
	//构造一个Quo实例
	var myQuo = new Quo(&quot;confused&quot;); //构造器模式调用
	document.writeln(myQuo.get_status());

	//构建一个包含两个数字的数组，并将他们相加
	add = function(a, b)
	{
		return a+b;
	}

	var anArray = [3, 4];
	var sum = add.apply(null, anArray);  //7

	//构建一个包含status成员的对象
	var statusObject = {
		status: &quot;A-OK&quot;
	};

	//statusObject并没有继承自Quo.prototype,但我们可以再statusObject上调用
	//get_status方法，尽管statusObject并没有一个名为get_status的方法。
	var status = Quo.prototype.get_status.apply(statusObject);
</pre>
<p>3. 参数：<br />
当一个函数被调用时，会有一个默认的参数，就是 arguments “数组”。</p>
<pre class="brush: jscript; title: ; notranslate">
	//构造一个将很多值相加的函数

	//注意该函数内部定义的变量sum不会与函数外部定义的sum产生冲突。
	//该函数只能看到内部的那个变量。
	var sum = function()
	{
		var i, sum=0;
		for(i = 0; i &lt; arguments.length; i+=1)
		{
			sum += arguments[i];
		}
		return sum;
	};

	document.writeln(sum(4, 8, 15, 16, 23, 42)); //108
</pre>
<p>注：arguments并不是一个真正的数组。它知识一个“类似数组”的对象。arguments用友一个length属性，但它缺少所有的数组方法。</p>
<p>4. 返回:<br />
一个函数总是会返回一个值。如果没有置顶返回值，则返回undefined。<br />
如果函数以构造器方式被调用，且返回值不是一个对象，则返回this(该新对象)。</p>
<p>5.给类型增加方法：</p>
<pre class="brush: jscript; title: ; notranslate">
	Function.prototype.method = function(name, func)
	{
		this.prototype[name] = func;
		return this;
	}

	//给Number添加一个integer方法，来提取数字钟的整数部分
	//根据正负来判断是使用Math.ceil还是Math.floor
	Number.method('integer', function(){
	 return Math[this &lt; 0 ? 'ceil' : 'floor'](this);
	});

	document.writeln((-10/3).integer()); //-3

	//去掉字符串两端空白的方法
	String.method('trim', function()
	{
		return this.replace(/^\s+|\s+$/g, '');
	});
</pre>
<p>基本类型的原型是公共结构，所以在类库混用时务必小心。一个保险的做法就是只在确定没有该方法时再添加：</p>
<pre class="brush: jscript; title: ; notranslate">
	//有条件的增加一个方法

	Function.prototype.method = function(name, func)
	{
		if(!this.prototype[name])
		{
			this.prototype[name] = func;
			return this;
		}
	};
</pre>
<p>6.闭包<br />
理解内部函数能访问外部函数的实际变量而无需<strong>复制</strong></p>
<pre class="brush: jscript; title: ; notranslate">
//构造一个函数，用错误的方式给一个数组中的借点设置事件处理程序
//当点击一个节点时，按照预想应该弹出一个对话框显示节点的序号
//但他总是会显示节点的数目

var add_the_handlers = function(nodes) {
	var i;
	for (i = 0; i &lt; nodes.length; i += 1) {
			nodes[i].onclick = function(e) {
				alert(i);
			}
	}
}
</pre>
<p>add_the_handlers函数的目的是给每个事件处理器一个唯一值（i）。它未能达到目的的原因是事件处理器函数绑定了变量i，而不是<strong>函数在构造时的变量i的值</strong>。<br />
正确的写法：</p>
<pre class="brush: jscript; title: ; notranslate">
//点击一个节点，将会弹出一个对话框显示节点的序号
var add_the_handlers = function (nodes) {
	var i;
	for (i = 0; i &lt; nodes.length; i +=1 ) {
		nodes[i].onclick = function (i) {
			return funtion (e) {
				return alert(e);
			} ;
		}(i);
	}
}
</pre>
<p>现在，定义了一个函数并立即传递i进去执行， 而不是把一个函数赋值给onclick。那个函数将返回一个事件处理器函数。这个事件处理器函数绑定的是传递进去的i的值，而不只定义在add_the_handlers函数里的i的值。那个被返回的函数被复制给onclick。</p>
<p>7.javascript的单例模式<br />
Javascript的单例就是用对象字面量表示法创建的对象，对象的属性值可以是数值或函数，并且属性值在该对象的生命周期中不会发生变化。他通常作为<b>工具为程序其他部分提供功能支持</b></p>
<p>8.模块<br />
模块模式的一般形式是：一个定义了<b>私有变量和函数的函数</b>；利用<b>闭包</b>创建可以访问<b>私有变量和函数</b>的特权函数,或者把他们保存到一个可访问的地方。</p>
<pre class="brush: jscript; title: ; notranslate">
String.method('deentityify', function (){
	var entity = {
		quot: '&quot;',
		lt: '&lt;',
		gt: '&gt;'
	}

	return function () {
		return this.replace(/&amp;([^&amp;;]+);/g,
			function (a, b) {
				var r = entity[b];
				return typeof r === 'string' ? r : a;
		});
	};
}());
</pre>
<p>模块模式利用了函数作用域和闭包来创建绑定对象与私有成员关联，摒弃了全局变量的使用，从而缓解这个JavaScript的最为糟糕的特性之一所带来的影响</p>
]]></content:encoded>
			<wfw:commentRss>http://www.smartwei.com/javascript-good-part-note-function.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>《javascript精粹》读书笔记 &#8211; 对象</title>
		<link>http://www.smartwei.com/javascript-good-part-note.html</link>
		<comments>http://www.smartwei.com/javascript-good-part-note.html#comments</comments>
		<pubDate>Sun, 09 Jan 2011 05:29:33 +0000</pubDate>
		<dc:creator>10V</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[javascript精萃]]></category>

		<guid isPermaLink="false">http://www.smartwei.com/?p=302</guid>
		<description><![CDATA[整理了《javascript精粹》中关于对象的一些内容]]></description>
			<content:encoded><![CDATA[<p>《javascript精粹》中和对象相关的内容：</p>
<p>1. 当尝试检索对象的一个并不存在的成员元素的值，将返回一个undefined值。可以通过“||”运算符来填充默认值：</p>
<pre class="brush: jscript; title: ; notranslate">
stooge[&quot;middle-name&quot;] //undefined
var middle = stooge[&quot;middle-name&quot;] || &quot;tom&quot;;
</pre>
<p>2. 当尝试检索一个undefined值将会导致TypeError异常。可以通过“&#038;&#038;”运算符来避免错误：</p>
<pre class="brush: jscript; title: ; notranslate">
flight.equipment  //undefined
flight.equipment.model //throw &quot;TypeError&quot;
flight.equipment &amp;&amp; flight.equipment.model //undefined
</pre>
<p>3. 对象通过引用来传递。他们永远不会被拷贝</p>
<pre class="brush: jscript; title: ; notranslate">
var x = stooge;
x.nickname = 'Curly';
var nick = stooge.nickname;
//因为x和stooge是指向同一个对象的引用，所以nick的值为'Curly'

var a={}, b={}, c={};
//a, b和c每个都引用一个&lt;strong&gt;不同&lt;/strong&gt;的空对象

a=b=c={}
//a,b和c都引用&lt;strong&gt;同一个&lt;/strong&gt;空对象
</pre>
<p>4. 原型连接<br />
通过beget方法创建一个使用原对象作为其原型的新对象</p>
<pre class="brush: jscript; title: ; notranslate">
if (typeof Object.beget !== 'function')
{
    Object.beget = function (o)
    {
        var F = function () {};
        F.prototype = o;
        return new F();
    }
}

var another_stooge = Object.beget(stooge);
</pre>
<p><strong>注：</strong><br />
a. 原型连接在更新时是不起作用的<br />
当我们对某个对象作出改变时，不会触及到该对象的原型。<br />
b. 原型连接只有在检索值的时候才被用到<br />
如果尝试去获取对象的某个属性值，且该对象没有此属性名，那么从它的圆形对象中获取属性值。 （如果那个原型对象也没有该属性值，那么再从它的原型中寻找，依次类推，知道该过程左后的重点Object.prototype。如果想要的属性完全不存在于原型链中，那么结果就是<strong>undefined</strong>值。这个过程称为<strong>委托</strong>）<br />
c. 原型关系是一种动态关系。<br />
如果我们添加一个新的属性到原型中，该属性会立即对所有基于该原形创建的对象可见。</p>
<p>5. 反射<br />
在一些时候下，我们需要能够在对对象完全不了解的情况下进行处理，并且在处理之前发现他们的属性和方法，这个过程称作反射（reflection）。<br />
在判断对象是否有某一属性时，可以使用”<strong>hasOwnProperty</strong>“方法，如果对象拥有独有的属性，它将返回true。<br />
注：hasOwnProperty方法不会检查原型链。</p>
<p>6. 枚举<br />
for in 语句可以用来遍历一个对象中的所有属性名。<br />
属性名出现的顺序是不确定的，因此要对任何可能出现的顺序有所准备。如果你想要确保属性以特定的顺序出现，最好的办法就是完全避免使用for in语句，而是创建一个数组，在其中以正确的顺序包含属性名：</p>
<pre class="brush: jscript; title: ; notranslate">
//使用for in语句：
	var name;
	for (name in another_stooge)
	{
		if(typeof another_stooge[name] !== 'function')
		{
			document.writeln(name + ': ' + another_stooge[name]);
		}
	}

//使用for语句
	var i;
	var properties = [
		'first-name',
		'middle-name',
		'last-name',
		'profession'
	]
	for (i=0; i&lt;properties.length; i +=1)
	{
		document.writeln(properties[i] + ': ' +
					another_stooge[properties[i]]);
	}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.smartwei.com/javascript-good-part-note.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>利用 PHP 5.3 的 lambdas 和 closures</title>
		<link>http://www.smartwei.com/php-lambdas-closures.html</link>
		<comments>http://www.smartwei.com/php-lambdas-closures.html#comments</comments>
		<pubDate>Thu, 06 Jan 2011 16:18:49 +0000</pubDate>
		<dc:creator>10V</dc:creator>
				<category><![CDATA[php知识]]></category>

		<guid isPermaLink="false">http://www.smartwei.com/?p=298</guid>
		<description><![CDATA[了解如何使用 PHP 5.3 的 lambdas 和 closures。寻找您考虑使用它们的原因以及降低代码的复杂性的方法。最后，了解 closure 词法范围变量的行为。]]></description>
			<content:encoded><![CDATA[<p>了解如何使用 PHP 5.3 的 lambdas 和 closures。寻找您考虑使用它们的原因以及降低代码的复杂性的方法。最后，了解 closure 词法范围变量的行为。<br />
<a href="http://www.ibm.com/developerworks/cn/opensource/os-php-lambda/?ca=drs-tp4608" title="php lambdas 和 php closures">详细内容</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.smartwei.com/php-lambdas-closures.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>2011年了</title>
		<link>http://www.smartwei.com/2011.html</link>
		<comments>http://www.smartwei.com/2011.html#comments</comments>
		<pubDate>Sat, 01 Jan 2011 10:00:55 +0000</pubDate>
		<dc:creator>10V</dc:creator>
				<category><![CDATA[未分类]]></category>

		<guid isPermaLink="false">http://www.smartwei.com/?p=296</guid>
		<description><![CDATA[2010年： 1.写了100篇文章 2.换了一份工作 3.渐渐习惯了在linux平台上开发 4.体重先减了10斤又弹了回去 &#8211; -! 5.看了了指定的小说 2010年憧憬： 1.健康的瘦到150-160 2.对javascript的使用可以达到前端工程师的水平 3.更多的关注一下php代码的性能，形成自己跟踪、分析bug的风格 4.灵活使用linux进行日常事务处理 5.提高自己的收入]]></description>
			<content:encoded><![CDATA[<p>2010年：<br />
1.写了100篇文章<br />
2.换了一份工作<br />
3.渐渐习惯了在linux平台上开发<br />
4.体重先减了10斤又弹了回去 &#8211; -!<br />
5.看了了指定的小说</p>
<p>2010年憧憬：<br />
1.健康的瘦到150-160<br />
2.对javascript的使用可以达到前端工程师的水平<br />
3.更多的关注一下php代码的性能，形成自己跟踪、分析bug的风格<br />
4.灵活使用linux进行日常事务处理<br />
5.提高自己的收入</p>
]]></content:encoded>
			<wfw:commentRss>http://www.smartwei.com/2011.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>array_merge, array_splice, array_slice使用时需要注意的地方</title>
		<link>http://www.smartwei.com/php-array-attention.html</link>
		<comments>http://www.smartwei.com/php-array-attention.html#comments</comments>
		<pubDate>Mon, 20 Dec 2010 09:59:53 +0000</pubDate>
		<dc:creator>10V</dc:creator>
				<category><![CDATA[php知识]]></category>

		<guid isPermaLink="false">http://www.smartwei.com/?p=293</guid>
		<description><![CDATA[array_merge, array_splice, array_slice使用使用数字做键，返回结果错误的原因]]></description>
			<content:encoded><![CDATA[<p>今天在灌数据的时候，发生了一个十分奇怪的现象，看下代码:</p>
<pre class="brush: php; title: ; notranslate">
$a = array
        (
            '1008/1009' =&gt; 1,
            '6065' =&gt; 1,
            '1002' =&gt; 1,
            'K1024/K1021' =&gt; 1,
        );

array_slice($a, 3);
var_dump($a);
</pre>
<p>期待的结果是：</p>
<pre class="brush: xml; title: ; notranslate">
array(3) {
  [&quot;1008/1009&quot;] =&gt; int(1)
  [&quot;6065&quot;] =&gt; int(1)
  [&quot;1002&quot;] =&gt; int(1)
}
</pre>
<p>而实际上的输出结果会变成：</p>
<pre class="brush: xml; title: ; notranslate">
array(3) {
  [&quot;1008/1009&quot;] =&gt; int(1)
  [0] =&gt; int(1)
  [1] =&gt; int(1)
}
</pre>
<p>可以看到数组$a中以数字最为key的项发生了变化，详细看了一下手册发现:<br />
<b>如果数组包含数字键名，array_splice将重置数组的键。</b> 这一现象在array_merge中也会发生。</p>
<p>解决的办法有很多种，可以使用array_slice替换array_splice。<strong>array_slice(</strong> array $array , int $offset [, int $length [, bool $preserve_keys ]] <strong>)</strong> 默认也会重置数组的键。自 PHP 5.0.2 起，可以通过将 preserve_keys 设为 TRUE 来改变此行为。 </p>
<p>如果是在使用array_merge是出现这个问题，则可以直接使用”+”来代替</p>
<p>作者: <a href="http://www.smartwei.com">石巍</a><br />
原载: <a href="http://www.smartwei.com/php-array-attention.html">10V的博客</a><br />
版权所有，转载时必须以超链接形式注明作者和原始出处及本声明。</p>
<p>本文链接: <a href="http://www.smartwei.com/php-array-attention.html">http://www.smartwei.com/php-array-attention.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.smartwei.com/php-array-attention.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

