`
jianglanlan
  • 浏览: 13243 次
  • 性别: Icon_minigender_1
  • 来自: 杭州市
社区版块
存档分类
最新评论

for in arguments/array

阅读更多

        在项目前端JS开发中,无意中有段代码去遍历arguments(入参对象), arguments为一个参数对象,即我们可以通过arguments[0],arguments[1]去访问对应的第N个参数(刚开始我认为它是一个数组)。在代码中,我是通过for in 去遍历arguments对象的,在chrome,firefox,ie9+都是支持的。然后在ie7等低版本IE7则就哑了。然而在下图代码中,在IE7中遍历数组却是正常的。

var arr = ["12", "test"];  
for (var i in arr) {  alert(i); } //依次打印出0,1(即数组的下标)

     这个时候我却茫然了,这个是怎么回事呢?难道是arguments对象比较特殊?通过$.type(arguments)查看其返回结果得知,arguments非数组。再到见多上查询:arguments并非真正的数组,它是一个Arguments对象。正好验证了我的想法。arguments 遍历的方法也跟数组遍历的方式一样,即for var遍历。

     另外,数组遍历最好用for var而非for in,否则会产生非预期的结果。那是因为for in 语句对数组对象进行遍历潜在的bug在于:如果原生Array类被其他的js脚本库进行了原型扩展(比如多加一个toJSON方法即Array.prototype.hello=xxxx),那么用for in遍历扩展后的Array对象的逻辑将与遍历原生Array对象的逻辑发生差异。

     按上图代码,如果Array是原生js类,那么结果就是我们预期的结果。但是,如果Array类被扩展了,多了一个hello方法,那么上面的语句将执行三次alert,//依次打印出0,1, hello(即数组的下标)。

     如果你设计的代码的逻辑以原生Array类为基准,在某一天你的同事在页面里面引用了一个第三方的JS库,这个库又恰好扩展了Array类,结果将难以想象,很有可能原来的代码逻辑将不再成立。 

        关于这种扩展原生JS类的库,很有名的一个就是prototype.js,它给Array类扩展了很多方法诸如toJSON,each等等。我现在明白为啥jquery的创始人曾经对prototype火大了(不少人因为特殊原因在一个页面里用jquery同时又用prototype,会有很多意料之外的冲突问题,仅仅一个noConflict是无法解决的)。

        所以,在数组遍历中用for var 代替for in。

分享到:
评论

相关推荐

    一个跨平台的CString源码

    // - Functional objects for use in STL algorithms // // From this template, we intstantiate two classes: CStdStringA and // CStdStringW. The name "CStdString" is just a #define of one of these, //...

    JavaScript实现的in_array函数

    当传入的参数不是数组时,如`null`,`in_array`函数会抛出一个异常,提示"arguments is not Array"。因此,在使用此函数时,确保传入的是有效的数组至关重要。 通过这样的自定义`in_array`函数,我们可以在...

    用js实现in_array的方法

    for (var i in array) { if (array[i] == search) { return true; } } return false; } alert(in_array(11, a)); ``` 第三个示例通过定义一个判断函数`in_array`,并在该函数中首先判断传入的参数是否为数组...

    06-for-in和for-of的区别.md

    JavaScript内置的可迭代对象包括`Array`、`String`、`Map`、`Set`、`arguments`对象和`NodeList`对象等。 ## 重点提示 - `for-in`主要用于对象(包括数组)的属性遍历,获取的是键(属性名或索引)。 - `for-of`...

    ButterFly:ButterFly 是一个支持多种标准的 Java 命令行参数解析器

    // construct array of strings to simulate command-line arguments// constrcut the Parser// the second parameter of POSIXParser are an array of arguments that you are interested in.Parser p = new ...

    jna-4.2.2 官方原版下载

    Java array and NIO Buffer arguments (primitive types and pointers) as pointer-to-buffer Nested structures and arrays Wide (wchar_t-based) strings Native long support (32- or 64-bit as appropriate)...

    inputparms.java源程序

    write a Java program called InputParms.java that accepts 3 arguments in the main method using an array. Iterate through the array looking for your name using a for loop. Display the message "The name ...

    Developing Statistical Software in Fortran 95

    The discussion of arrays, in particular, is extensive, covering operations on arrays and array sections, allocation, and more. #### Pseudo Object-Oriented Style The book delves into implementing a ...

    array.js:使ie8等浏览兼容array最新方法

    在IE8中,同样需要使用`Array.prototype.slice.call()`和for循环。 ```javascript if (!Array.prototype.filter) { Array.prototype.filter = function(callback, thisArg) { var T, A, k, value; if (this == ...

    Matlab有限元网格化源程序-ddd.m

    Matlab有限元网格化源程序-ddd.m MIT的人写的论文,一个简单的有限元网格化方法 确实可以用,但是我理解...• Additional parameters to the functions fd and fh can be given in the last arguments varargin .

    Matlab有限元网格化源程序-huniform.m

    Matlab有限元网格化源程序-huniform.m MIT的人写的论文,一个简单的有限元网格化方法 ...• Additional parameters to the functions fd and fh can be given in the last arguments varargin .

    Matlab有限元网格化源程序-dcircle.m

    Matlab有限元网格化源程序-dcircle.m MIT的人写的论文,一个简单的有限元网格化方法 ...• Additional parameters to the functions fd and fh can be given in the last arguments varargin .

    Matlab有限元网格化源程序-distmesh2d.m

    Matlab有限元网格化源程序-distmesh2d.m MIT的人写的论文,一个简单的有限元网格化方法 ...• Additional parameters to the functions fd and fh can be given in the last arguments varargin .

    bsearch.zip

    The contents of the array should be in ascending sorted order according to the comparison function refer‐ enced by compar. The compar routine is expected to have two arguments which point to the ...

    LCTF软件备份VariSpec™ Liquid Crystal Tunable Filters

    a) added support for multiple filters per process in VsDrvr.dll b) updated manual Previous release 1.3.5 Release date: October 11th, 2005 Known bugs: none Fixes/features added from previous release ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    In some cases there might be good arguments for changing certain style rules, but we nonetheless keep things as they are in order to preserve consistency. Another issue this guide addresses is that...

    Program in LUA 2nd Edition.rar

    15.2 The Basic Approach for Writing Modules 141 15.3 Using Environments 143 15.4 The module Function 144 15.5 Submodules and Packages 145 16 Object-Oriented Programming 149 16.1 Classes 151 16.2...

    王小平版遗传算法的光盘源代码

    ) passed into it as arguments, whereas a macro has an array of unevaluated expressions passed in, so that some of them may remain unevaluated to prevent side-effects. Here is how the properties ...

    i-vector的工具箱

    In case Matlab is not installed or Matlab license is not available (for instance on a computer cluster), we provide standalone executables that can be used in conjunction with the Matlab Compiler ...

Global site tag (gtag.js) - Google Analytics