简体   繁体   English

javascript自定义数组搜索原型出现了一些问题

[英]javascript custom array search prototype got some problems

I create an array search prototype 我创建一个数组搜索原型

Array.prototype.searchRE = function(searchStr) {   
    var returnArray = false;   
    for (i in this) { 
      if (typeof(searchStr) == 'object') {
        if (searchStr.test(this[i])) { 
          if (!returnArray) {returnArray = []} 
          returnArray.push(i);
        }
      } else {
        if (this[i] === searchStr) {
          if (!returnArray) {returnArray = []}
          returnArray.push(i);
        }
      }   
    }  
    return returnArray;
}

var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";

result1=mycars.searchRe(/bm/i);   // return 2 
result2=mycars.searchRe(/b/i);    // return 0,2,searchRe
result3=mycars.searchRe(/m/i);    // return 2

My questions is no 2, why it returns "searchRe"? 我的问题是2,为什么它返回“ searchRe”? the function name? 函数名称?

That happens because you are traversing you array with the for-in statement, and this statement enumerates inherited properties (as your searchRe function). 发生这种情况是因为您使用for-in语句遍历数组,并且该语句枚举了继承的属性(作为searchRe函数)。

The purpose of the for-in statement is to enumerate object properties, to traverse arrays a sequential loop is always recommended. for-in语句的目的是枚举对象属性,建议始终遍历顺序循环遍历数组。

...<snip>... ... <片段> ...

Edit: Ok, since you are handling other properties than "array indexes" in your array objects, you should check that the properties you enumerate are own, non-inherited : 编辑:好的,因为您正在处理数组对象中除“数组索引”之外的其他属性,所以应检查您枚举的属性是自己的, 非继承的

//...
for (var i in this) { // <- notice `var i`, otherwise `i` will be global
  if (this.hasOwnProperty(i)) { // the property physically exist on the object
    //..
  }
}
//..

Edit 2: You will have problems detecting when the argument is a RegExp object. 编辑2:当参数是RegExp对象时,您将遇到问题。

In Chrome, there is a syntax extension that allows you to "call" a RegExp object just like it were a function, Firefox allows this also, but in Chrome the real problem is that the typeof operator returns "function" instead the expected "object" result, for example: 在Chrome中,有一个语法扩展名,可以让您像调用函数一样“调用” RegExp对象,Firefox也允许这样做,但是在Chrome中,真正的问题是typeof运算符返回"function"而不是预期的"object"结果,例如:

var re = /a/;
re('a');   // equivalent to call re.exec('a');

typeof re; // "function" on Chrome, "object" in other implementations

So, instead checking: 因此,请检查:

//..
if (typeof searchStr == 'object') {
  //...
}
//..

I would recommend you at least: 我至少建议您:

if (searchStr && searchStr.test) {
  //...
}

Or: 要么:

if (searchStr && typeof searchStr.test == 'function') {
  //...
}

Or: 要么:

if (Object.prototype.toString.call(searchStr) == '[object RegExp]') {
  //...
}

It is because you are using an associative array loop (for i in this), which when applied to an object will loop over the properties and methods too. 这是因为您正在使用关联数组循环(对于本例中的i),将其应用于对象时也会循环遍历属性和方法。 In this case you are adding a "searchRE" method to the array, which qualifies it in the loop. 在这种情况下,您要向数组添加“ searchRE”方法,该方法可以在循环中进行限定。 Change your loop to a normal for loop. 将您的循环更改为普通的for循环。

for(var i = 0; i < this.length; i++) {

}

I have a blog post about js loops: http://slappyza.wordpress.com/2010/10/03/fast-loops-in-javascript/ 我有一篇有关js循环的博客文章: http : //slappyza.wordpress.com/2010/10/03/fast-loops-in-javascript/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM