比str+=str;高效的字符串连接方法

Posted on 2008-6-5 星期四

在javascript编程中经常要连接字符串,很多人都习惯用:

  1. var str=“hello”;
  2. str+=“world”;

这种操作在JS中要经过6个步骤,非常耗资源:
(1) 创建存储”hello”的字符串.
(2) 创建存储”world”的字符串.
(3) 创建存储连接结果的字符串.
(4) 把str的当前内容复制到结果中.
(5) 把”world”复制到结果中.
(6) 更新str,使它指向结果.

解决方法就是用array对象来连接字符串:

  1. var arr = new Array;
  2. arr[0] = “hello”;
  3. arr[1] = “world”;
  4. var str = arr.join(“”);

执行只需两步,
(1) 创建存储结果的字符串.
(2) 把每个字符串复制到结果中的合适位置.

下面为了方便使用,给出了一个字符串连接类:

  1. function SGen(){this._str_ = new Array;};
  2. SGen.prototype.add = function (str){this._str_.push(str);};
  3. SGen.prototype.toString = function (){return this._str_.join(“”);};
  4.  
  5. var buffer = new SGen();
  6. buffer.add(“hello “);
  7. buffer.add(“world”);
  8. var result = buffer.toString();
  9. buffer = null;
Dorian @ 11:47 上午
类归于: Javascript
多个IE版本共存(IEtester)

Posted on 2008-5-30 星期五

在web前端开发过程中,要保证页面的A-grade浏览器兼容性,通常我们要在多个浏览器版本中做测试。
常用的方式,装个multipleIE,或者装VM。现在推荐另一种解决方案:IEtester
这个软件,在一个窗口中同时可以打开从IE5.5-IE8.0 beta的各版本的tab窗口,同时测试一个页面的显示效果,兼容性。

下载地址:http://www.my-debugbar.com/wiki/IETester/HomePage

Dorian @ 10:19 上午
类归于: Software
Opera 下的”FireBug”-dragonfly

Posted on 2008-5-7 星期三

Opera 9.50 beta 版本加入了一个类似firefox 下的FireBug功能的,Web developer工具 dragonfly “龙舞”:http://www.opera.com/products/dragonfly/

Web developer 可以在opera下方便的调试自己的网页程序了.

http://www.opera.com/img/products/dragonfly/opera-dragonfly.jpg

Dorian @ 11:55 上午
类归于: Software