Javascript Array extension

后端 未结 3 1075
再見小時候
再見小時候 2021-01-28 11:47

Just wonder. I have extended javascript Array object using its prototype as follows:







        
3条回答
  •  悲&欢浪女
    2021-01-28 12:07

    A question would be helpful ;-)

    I assume you don't want the prototype property to show up? JS doesn't allow you to set "DontEnum" on properties you add to the prototype, so you have to check to see if the array has the property, or if it's a property of its prototype:

    for(var i in ax) {
        if (a.hasOwnProperty(i)) {
            document.write(ax[i]);
        }
    }
    

    although to iterate over an array you shouldn't really be using for...in as that's for iterating over the properties of an object, rather than the elements of an array.

提交回复
热议问题