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"){
}
}