Stopping enumeration in JavaScript when using prototype

牧云@^-^@ 提交于 2020-03-06 02:15:50

问题


I'm currently trying to get my head around using prototype in JavaScript.

To experiment with this, I've written a function that effectively works as allowing you to put a where clause onto arrays:

Array.prototype.where=(function(){
  var tmpArr=[],
      success;

  for (var x in this){
    var success=true;
    for (var i in arguments){
      if (this[x][arguments[i][0]]!=arguments[i][1]){
        success=false;
        break;
      }
    }

    if (success==true){
      tmpArr.push(this[x]);
    }
  }
  return tmpArr;
});

An example use would be:

arrayName.where([0, 'Fred'], [1, 'Bloggs']);

For the sake of a test, this works pretty well. The only problem is if you were then to run

for (var x in someArrayHere){
  console.log(someArrayHere[x]);
}

You get the output the array, but with a record representing the function you've prototyped.

As far as I can work out, this is sorted by setting the function as non-enumerable, but I can't find any articles explaining how to stop it.

How would I go about it? Or would I have to do the below each time?

for (var x in someArray){
  if (typeof tSch[x]!="object"){
  }
}

回答1:


The for...in construct enumerates all properties of an object (and those inherited down the prototype chain). When you use it with an array, it will iterate over all properties of Array.prototype, as well as the elements of the array.

Use a normal for loop to prevent this:

for(var x = 0; x < this.length; x++) {
    //Do stuff
}



回答2:


In general, using a for ... in loop to iterate through an array is bad practice.

However, a simple fix would be to use Object.hasOwnProperty:

for (var x in someArray) {
  if (someArray.hasOwnProperty(x)) {
    //...
  }
}

This is a more robust approach than your loop.




回答3:


In general, you should not mess with the prototypes of native Objects. This leads to problems - especially in combination with extern code/libraries.

Instead, you should make it a function that operates on an array parameter (for instance function arrayWhere(arr)).

If you want to stick to extending native prototypes, you should iterate over arrays with the forEach method (newer browsers, maybe add a fallback as described at the MDN), or the hasOwnProperty method of objects, as described in this example.



来源:https://stackoverflow.com/questions/8878456/stopping-enumeration-in-javascript-when-using-prototype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!