I have extended Array
prototype:
if(typeof Array.prototype.filter === 'undefined') Array.prototype.filter = function(fun /*, thisp*/){
var len = this.length;
if(typeof fun != "function") throw new TypeError();
var res = [], thisp = arguments[1];
for(var i=0;i<len;i++){
if(i in this){
var val = this[i]; // in case fun mutates this
if(fun.call(thisp, val, i, this)) res.push(val);
}
}
return res;
};
For example I have created array:
var A = [ 1, 2, 3, 4, 5 ];
Then I added extra properties to it, that I will use:
A.creator = 'Rustam';
A.created = new Date();
If I will use for-in
loop, and browser has not built-in support for Array.filter
, it will go through A.filter
.
I know this way:
for(var p in A) {
if (!A.hasOwnProperty(p)) continue
console.log(p)
};
Is there a way to make A.filter
hidden in for-in
without using hasOwnProperty
?
UPDATE to answer. Browser support:
- IE9+
- FF4+
- Chrome
- Opera 11.6+
- Safari 5+