[removed] hiding prototype methods in for loop?

后端 未结 6 1772
攒了一身酷
攒了一身酷 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.

提交回复
热议问题