javascript中编写类似in_array()的原型函数

来源:互联网 发布:宝宝听故事软件 编辑:程序博客网 时间:2024/05/17 08:03
Array.prototype.contains = function(obj) {    var i = this.length;    while (i--) {        if (this[i] === obj) {            return true;        }    }    return false;}或Array.prototype.contains = function(element) {    for (var i = 0; i < this.length; i++) {        if (this[i] == element) {            return true;        }    }    return false;}或Array.prototype.in_array = function(e) {    for(i=0; i<this.length && this[i]!=e; i++);    return !(i==this.length);}还有一个大牛是这样写的:Array.prototype.S = String.fromCharCode(2);Array.prototype.in_array = function(e) {    var r = new RegExp(this.S+e+this.S);    return (r.test(this.S+this.join(this.S)+this.S));}