近期今天在写一个“删除字符串中反复字符串”的函数,代码例如以下:

开门见山,重点

string.charAt(index) 取代 string[index]

function removeReapeatStrings1(str) {
var str = trim(str);
var len = str.length;
var once = str; if (len !== 0) {
var fromindex = 1;
var index = -1;
var i = 0; for (index = str.indexOf(str[0], fromindex); (index !== -1) && (2 * index <= len); ) {
// check the characters between 1 and index
for (i = 1; i < index; ++i) {
if (str[i] !== str[index + i]) {
break;
}
}
// if we found a unique string, stop for exit function
if (i === index) {
once = str.slice(0, index);
break;
}
// not found look for a same character as the first character of str
fromindex = index + 1;
index = str.indexOf(str[0], fromindex);
}
} return once;
}
alert(removeReapeatStrings1('北京'));
alert(removeReapeatStrings1('北京北京'));
alert(removeReapeatStrings1('大兴大地大兴大地'));

上面代码在非IE浏览器,执行正常。分别alert 北京,北京,大兴大地,但在IE下,奇迹发生了alert 北京, 北京北京。 大兴大地大兴大地。

왜?どうして?

于是各种调试(我用的是简单直观地alert调试法。不要见笑)

最后发现 str[0] 在万恶的IE下,居然是undefined!?所学知识在脑中不断闪现。似乎记起来在《High
Performance Javascript》最后几章有这种提示:不要用简短表达式取代原生函数(假设有对应的)。天哪?果断所有的string[index],所实用string.charAt(index)取代。

正确兼容IE和非IE代码例如以下:

function removeReapeatStrings1(str) {
var str = trim(str);
var len = str.length;
var once = str; if (len !== 0) {
var fromindex = 1;
var index = -1;
var i = 0; for (index = str.indexOf(str.charAt(0), fromindex); (index !== -1) && (2 * index <= len); ) {
// check the characters between 1 and index
for (i = 1; i < index; ++i) {
if (str.charAt(i) !== str.charAt(index + i)) {
break;
}
}
// if we found a unique string, stop for exit function
if (i === index) {
once = str.slice(0, index);
break;
}
// not found look for a same character as the first character of str
fromindex = index + 1;
index = str.indexOf(str.charAt(0), fromindex);
}
} return once;
}

贴一段文字。from http://stackoverflow.com/questions/5943726/string-charatx-or-stringx

There are two ways to access an individual character in a string. The first is the charAt method:

return 'cat'.charAt(1); // returns "a"

The other way is to treat the string as an array, where each index corresponds to an individual character:

return 'cat'[1]; // returns "a"

The second way (treating the string as an array) is not part of ECMAScript 3; it's a JavaScript and ECMAScript 5 feature (and not supported in all browsers).

In both cases, attempting to set an individual character won't work. Trying to set a character through charAt results in an error, while trying to set a character via indexing does not throw an error, but the string itself is unchanged.

So, as you might have figured out by now, charAt() is
better from a compatibility perspective.

String.charAt() is the standard and it works in all the browsers. In non-IE browsers you may use bracket notation to access characters but IE doesn't support it. (Not sure whether they have implemented that with the latest versions).

If somebody really wants to use bracket notication. It's wise to convert the string to char array in order to make it compatible with any browser.

面对字符串,请谦卑地使用charAt吧。理由

1、正确性

2、兼容性

全篇完结。

WHY IE AGAIN? - string.charAt(x) or string[x]?的更多相关文章

  1. java基础进阶一:String源码和String常量池

    作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/8046564.html 邮箱:moyi@moyib ...

  2. String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";作用!!!!!

    <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+re ...

  3. String使用机制及string.equals()和==的区别(转)

    http://904582819.blog.163.com/blog/static/11159282020127794456840/ equals方法和==的区别   首先大家知道,String既可以 ...

  4. String的高级用法(String.Format)

    string.Format C#的String.Format的一般地我们可以直接使用string.format()或int.ToString()和float.ToString() 下面是一些Strin ...

  5. .NET中string[]数组和List<string>泛型的相互转换以及Array类的Sort()方法(转)

    从string[]转List<string>: " }; List<string> list = new List<string>(str); 从List ...

  6. convert NameValueCollection/Dictionary<string, object> to JSON string

    public static class WebExtension { public static T Decode<T>(this RequestBase res) { Type type ...

  7. 深入解析字符串的比较方法:“==”操作符;String.Equals方法;String.Compare方法;String.CompareOrdinal方法。

    1:要判断2个字符串变量是否相等,最高效的方法是看它们是否指向相同的内存地址.前面使用RefernceEquals方法来比较.如果2个变量指向的是不同的内存地址,那么就需要逐字符的比较2个字符串的变量 ...

  8. Library string type(2)——关于String的操作

    关于string的定义,请参阅博文http://blog.csdn.net/larry233/article/details/51483827 string的操作 s.empty() //Return ...

  9. String类比较,String类运算比较,String运算

    String类比较,String类运算比较 >>>>>>>>>>>>>>>>>>>&g ...

随机推荐

  1. 颜色矩阵 滤镜 ColorMatrix

    颜色矩阵原理 色彩的三要素 1.色相.色相通俗的说就是"颜色",色相的改变就是颜色的改变,色相的调节伴随着红橙黄绿蓝紫的变化. 2.亮度.明度通俗的说就是"光照度&quo ...

  2. css伪类选择器详细解析及案例使用-----伪类选择器(1)

    动态伪类选择器:E:link :选择匹配的E元素,并且匹配元素被定义了超链接并未被访问过.E:visited :选择匹配的E元素,而且匹配的元素被定义了连接并已被访问过.E:active :选择匹配的 ...

  3. IDEA - Project files cannot be watched (are they under network mount?)

    在64位Linux系统上使用IDEA时遇到如下问题,启动时警告信息External file changes sync may be slow Project files cannot be watc ...

  4. eclipse中安装genymotion

    在eclipse中安装genymotion.安装genymotion需要先安装virtualbox.选择Help选项中的install new software 然后点击进去点击ADD,在locati ...

  5. iOS_SN_地图的使用(2)

    上一篇讲的是地图的基本使用,和注意事项,这一篇主要讲POI检索.百度地图SDK提供三种类型的POI检索:周边检索.区域检索和城市内检索.下面将以周边检索为例,向大家介绍如何使用检索服务. - (voi ...

  6. easyui之datagrid(不定时补充)

    1,datagrid之formatter formatter格式化函数有三个参数: value:字段值(一般为后台传递给前台的值): row:当前行数据: index:当前行索引. return值是显 ...

  7. ZOJ3551 Bloodsucker(概率dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Bloodsucker Time Limit: 2 Seconds      Me ...

  8. sublime3配置及插件安装

    1.下载https://github.com/wbond/sublime_package_control中的zip文件,解压后将文件夹名更改为Package Control. 2.将1中的文件夹放入s ...

  9. 小巧实用的数字加减插件(jquery插件)

    2015-12-04 近期项目需要,我将插件更新了,增加了两个参数,一个参数控制文本框是否支持输入,另一个参数则是新增了一个回调函数,返回文本框内的值.另外对代码局部重构了,优化了一下封装,需要的朋友 ...

  10. 解决表格里面使用text-overflow后依旧不能隐藏超出的文本

    解决表格里面使用text-overflow后依旧不能隐藏超出的文本 来源: http://blog.csdn.net/colinmuxi/article/details/9069595  (非原创,自 ...