Javascript数组扩展 [英] Javascript Array extension

查看:68
本文介绍了Javascript数组扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道。
我使用其原型扩展了javascript数组对象,如下所示:

Just wonder. I have extended javascript Array object using its prototype as follows:

<html>
<head>
</head>
<body>
<script type="text/javascript">
  function SomeMethod(){
  alert('Hello');
  }
  if(typeof Array.prototype.SomeMethod ==='undefined' ){
    Array.prototype.SomeMethod = SomeMethod;
  }
  var ax=new Array("A","B","C");
  for(var i in ax){
    document.write(ax[i]);
  }
</script>

</body>
</html>

结果将是:

ABCfunction SomeMethod() { alert("Hello"); } 

编辑:
虽然我已经找到答案但我觉得有必要添加一些
更多信息,以便其他人更清楚。

Although I have already found the answer I feel the necessity to add some more information so it would be clearer for others.

推荐答案

for..in 迭代对象的(非内置)属性。不要用它来遍历数组。只需使用常规进行循环。

for..in iterates over the (non built-in) properties of an object. Do not use it to iterate through an array. Just use a regular for loop.

阅读此链接 https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/for...in#说明

这篇关于Javascript数组扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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