[removed] hiding prototype methods in for loop?

后端 未结 6 1752
攒了一身酷
攒了一身酷 2020-12-01 05:14

So lets say I\'ve added some prototype methods to the Array class:



Array.prototype.containsKey = function(obj) {
    for(var key in this)
        if (key =         


        
相关标签:
6条回答
  • 2020-12-01 05:37

    You can use JavaScript's hasOwnProperty method to achieve this in the loop, like this:

    for(var key in arr) {
        if (arr.hasOwnProperty(key)) {
            ...
        }
    }
    

    Reference: This YUI blog article.

    0 讨论(0)
  • 2020-12-01 05:38

    You can achieve desired outcome from the other end by making the prototype methods not enumerable:

    Object.defineProperty(Array.prototype, "containsKey", {
      enumerable: false,
      value: function(obj) {
          for(var key in this)
            if (key == obj) return true;
          return false;
        }
    });
    

    This usually works better if you have control over method definitions, and in particular if you have no control over how your code will be called by other people, which is a common assumption in library code development.

    0 讨论(0)
  • 2020-12-01 05:41

    you could do this:

    for(var key in arr)
    {
       if (typeof(arr[key]) == "function")
          continue;
       alert(key);
    }
    

    But that's a shoddy workaround

    0 讨论(0)
  • 2020-12-01 05:41

    For high-performance iteration over JavaScript arrays, use either a for or while loop. Nicholas Zakas discusses the most-performant options for iterating over arrays in his Tech Talk Speed Up Your JavaScript.

    Your best bet is probably something like this:

    for (var i = collection.length - 1; i >= 0; i--) {
      if (obj == collection[i]) return true;
    }
    

    This approach will be peform best for a few reasons:

    • Only a single local variable is allocated
    • The collection's length property is only accessed once, at the initialization of the loop
    • Each iteration, a local is compared to a constant (i >= 0) instead of to another variable
    0 讨论(0)
  • 2020-12-01 05:46

    Javascript doesn't support associative arrays the way you think they do. http://ajaxian.com/archives/javascript-associative-arrays-considered-harmful

    for (var i in .. gets all of the properties of an object (an array is just another object) which is why you're seeing the other objects you've prototyped to it.

    As the article suggests you should use an object:

    
    var assoc = {'One' : 1, 'Two' : 2};
    assoc['Three'] = 3;
    
    for(var key in assoc)
       alert(key+' => '+assoc[key]);
    
    0 讨论(0)
  • 2020-12-01 05:52

    Method 1: use Object.keys (which doesn't return prototype properties) & loop

    Object.keys(arr); // ['One', 'Two', 'Three']
    Object.keys(arr).forEach(key => console.log(key))
    

    Method 2: hasOwnProperty inside a for-loop.

     for(var key in arr) {
       if (arr.hasOwnProperty(key)) {
         ...
       }
     }
    
    0 讨论(0)
提交回复
热议问题