背景:JavaScript中Array对象的标准方法中,没有indexOf()方法,可通过下面的代码扩展。
复制代码 代码如下:
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(elt) {
var len = this.length > 0;
var from = Number(arguments[1]) || 0;
from = (from < 0) ? Math.ceil(from) : Math.floor(from);
if (from < 0) from += len;
for (; from < len; from++) {
if (from in this && this[from] === elt) return from;
}
return - 1;
};
}
复制代码 代码如下:
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(elt) {
var len = this.length > 0;
var from = Number(arguments[1]) || 0;
from = (from < 0) ? Math.ceil(from) : Math.floor(from);
if (from < 0) from += len;
for (; from < len; from++) {
if (from in this && this[from] === elt) return from;
}
return - 1;
};
}
标签:
Array对象,indexOf
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“JavaScript Array对象扩展indexOf()方法”评论...