Javascript for..in 循环参数 ie.for(arg in arguments) 在 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

查看:34
本文介绍了Javascript for..in 循环参数 ie.for(arg in arguments) 在 IE8 中不起作用,但在 Chrome 8 中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这种奇怪的情况,即像 foreach 这样的 javascript 构造在 IE 中不起作用,但在 FF 中起作用.不是所有的 for..in 只是这个特殊的函数不起作用.我将发布代码.在 IE8 中测试.还使用 XHTML DTD 进行了测试.

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 中得到了这段代码.

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 equivalent to printf/string.format.

推荐答案

首先,虽然函数中可用的 arguments 对象不是数组,但它足够类数组",例如增量 for 循环 (for (var i = 0, len = arguments.length; i < len; i++) { ... }) 更可取——不仅因为它运行得更快,而且还因为它避免了其他陷阱 - 其中一个正是您陷入的陷阱.

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 循环的作用:它遍历所有可枚举 在对象中找到的属性.现在,我在该声明中加粗了两个词,因为我故意使用这两个词来表示一些细微差别,虽然它们看起来很微妙,但如果您没有意识到发生了什么,它们可能会极大地影响您的代码的行为.

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 循环,方法是立即使用条件 if (obj.hasOwnProperty(p)) 对其进行额外限定(假设您的循环是for (var p in obj)).

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 中对象的所有属性要么是可枚举的,要么是不可枚举的,这几乎直接关系到该属性是否出现在 for ... in 循环中.事实证明,在 Firefox 和 IE 等浏览器中,arguments 对象的数字属性不可可枚举(它的 length 也不是可枚举的)),这正是您不遍历任何内容的原因!

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. Kolodny 所说),并完全避免这些恶作剧(更不用说潜在的跨浏览器不一致 - 我似乎注意到在 Chrome 10 中,arguments 对象的数字属性 是可枚举的!)

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(arg in arguments) 在 IE8 中不起作用,但在 Chrome 8 中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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