使用Javascript的for..in遍历参数ie.for(阿根廷在参数)不能在IE8工作,但它在Chrome 8的工作原理 [英] Javascript for..in looping over arguments ie.for( arg in arguments) does not work in IE8 but it works in Chrome 8

查看:107
本文介绍了使用Javascript的for..in遍历参数ie.for(阿根廷在参数)不能在IE8工作,但它在Chrome 8的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这种奇怪的情况是类似的JavaScript构建的foreach并不在IE工作,但在FF的作品。那么不是所有的的for..in 只是这个特殊的功能可按不起作用。我将张贴code。经测试,在IE8。与DTD XHTML还测试。

I faced this strange situation where foreach like construct of javascript does not work in IE but it works in FF. Well not all for..in just this special funciton does not work. I will post the code. Tested in IE8. Tested also with XHTML DTD.

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
 <HTML>
  <HEAD>
   <TITLE> Test </TITLE>
   <META NAME="Generator" CONTENT="EditPlus">
   <META NAME="Author" CONTENT="">
   <META NAME="Keywords" CONTENT="">
   <META NAME="Description" CONTENT="">
  </HEAD>
 <script type="text/javascript">
 <!--
  String.prototype.format = function() {         
   var formatted = this;       
   var mycars = new Array(); //some 
   mycars[0] = "Saab";
   mycars[1] = "Volvo";
   mycars[2] = "BMW";
    var arg;
     for (arg in mycars) {        alert('it comes here');     
      formatted = formatted.replace("{" + arg + "}", arguments[arg]);         }         
      return formatted;
     };

  String.prototype.format2 = function() {         
  var formatted = this;       
  var arg;
     for (arg in arguments) {        alert('it does not come here');     
      formatted = formatted.replace("{" + arg + "}", arguments[arg]);         }         
      return formatted;    
  };

 function fn() {
  var s = 'The {0} is dead. Don\'t code {0}. Code {1} that is open source!'.format('ASP', 'PHP'); 
  alert('format:'+s);
   var s2 = 'The {0} is dead. Don\'t code {0}. Code {1} that is open source!'.format2('ASP', 'PHP'); 
 alert('format2:'+s2); //does not replace {0}s and {1}s
 } 
 //-->
 </script>
  <BODY>
  <input type="button" value="click " onclick="fn();" />

  </BODY>
 </HTML>

更新我发布了一个错误的问题,它在Firefox,但不是在IE8这是错误的。它不工作在Firefox太多。其实我从后的JavaScript的printf /的String.Format 这code。

Update I posted a wrong question that it works in FireFox but not in IE8 which was wrong. It does not work in FireFox too. Actually I got this code from post Javascript printf/string.format.

推荐答案

首先,而参数在函数中可用的对象不是一个数组,它是阵列状到足以使一个增量的for循环(为(VAR I = 0,LEN =与arguments.length; I&LT; LEN,我++){...} )是preferable - 不仅因为它的运行速度更快,而且还因为它避免了其他缺陷 - 其中之一就是你落入什么

First of all, while the arguments object available within a function is not an array, it is "array-like" enough such that an incremental for loop (for (var i = 0, len = arguments.length; i < len; i++) { ... }) is preferable - not only because it runs faster, but also because it avoids other pitfalls - one of which is exactly what you're falling into.

要真正回答问题的为什么的第二个循环是不行的,要实现正是for ... in循环确实是很重要的:它通过迭代的所有可枚举在对象中发现的属性。现在,我已经在加粗的声明中2个字,因为我用这两个词有目的地指示一对夫妇的细微差别,虽然他们看似细微,可以大大影响你的code的行为,如果你不知道这是怎么回事。

To actually answer the question of why the second loop doesn't work, it's important to realize just what for ... in loop does: it iterates through all enumerable properties found in an object. Now, I've bolded 2 words in that statement, because I used these two words purposefully to indicate a couple of nuances that, while they may seem subtle, can drastically affect the behavior of your code if you don't realize what's going on.

首先,让我们专注于全部 - 我的意思是说,不只是物体本身的性能外,还具有潜在的属性表示对象已经从它的原型,或者其原型的原型,还是让继承上。出于这个原因,它经常建议后卫的任何for ... in循环,立马用另外的条件限定它如果(obj.hasOwnProperty(P))(假设你的循环被写入在OBJ(VAR p))。

First let's focus on all - by which I mean to say, not just properties of the object itself, but also potentially properties said object has inherited from its prototype, or its prototype's prototype, or so on. For this reason, it is very often recommended that you "guard" any for ... in loop by immediately additionally qualifying it with the condition if (obj.hasOwnProperty(p)) (assuming your loop were written for (var p in obj)).

但是,这不是你正在运行的进这里。对于这一点,让我们集中于第二个字,枚举。在JavaScript对象的所有属性是枚举或不可枚举,其中大部分pretty直接关系到物业是否显示了一个for ... in循环与否。在浏览器如Firefox和IE浏览器,事实证明,在参数对象的数值属性的的枚举(也不是它的长度因为它是),这是precisely你为什么不通过任何迭代!

But that's not what you're running into here. For that, let's focus on that second word, enumerable. All properties of objects in JavaScript are either enumerable or non-enumerable, which pretty much directly relates to whether the property shows up in a for ... in loop or not. In browsers such as Firefox and IE, as it turns out, the arguments object's numeric properties are not enumerable (nor is its length as it were), which is precisely why you're not iterating through anything!

不过说真的,到了最后,通过任何为数组或类似数组的迭代,你最好使用增量环(如M.科劳德尼还指出),并完全避免这些把戏(更不用说潜在的跨浏览器不一致 - 我似乎注意到,在Chrome 10的参数的数值属性对象的的枚举)

But really, in the end, for iterating through anything that is an Array or array-like, you are better off using an incremental loop (as M. Kolodny also stated), and avoiding these shenanigans altogether (not to mention potential cross-browser inconsistencies - I seem to be noticing that in Chrome 10, the numeric properties of arguments objects are enumerable!)

这篇关于使用Javascript的for..in遍历参数ie.for(阿根廷在参数)不能在IE8工作,但它在Chrome 8的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆