I am not new to programming but things like closure and callback functions strike me hard. They are new ways of thinking, guess I have to learn alot.
My question is, how can this work? I mean, we are calling a function without giving the parameters while this function needs three parameters? Code snippet is here, anf the whole script is following it.
....
testedColors[i] = colors[i].filter(checkColor);
.....
function checkColor(element,index,array) {
return (element >= 0 && element < 256);
--------------------------------------------------------------------------
Whole code is here:
// check color range callback function
function checkColor(element,index,array) {
return (element >= 0 && element < 256);
}
// check to ensure three RGB colors
function checkCount(element,index,array) {
return (element.length == 3);
}
function testingCallbacks() {
// color array
var colors = new Array();
colors[0] = [0,262,255];
colors[1] = [255,255,255];
colors[2] = [255,0,0];
colors[3] = [0,255,0];
colors[4] = [0,0,255];
colors[5] = [-5,999,255];
colors[6] = [255,255,1204556];
// filter on color range
var testedColors = new Array();
for (var i in colors) {
testedColors[i] = colors[i].filter(checkColor);
}
// print results of first round
document.writeln("<h3>First check</h3>");
for (i in testedColors) {
document.writeln(testedColors[i] + "<br />");
}
// filter fewer than three values
var newTested = testedColors.filter(checkCount);
document.writeln("<br /><h3>Second</h3>");
// print survivors
for (i in newTested) {
document.writeln(newTested[i] + "<br />");
}
}
//]]>
</script>
</head>
<body onload="testingCallbacks();">
Just remember that the checkColor function is not being called here, because there are no () at the end. The checkColor function is being passed as an argument to filter. filter will then call the checkColor function internally passing the needed parameters.
As far as I'm aware, filter is a FireFox only feature?
As far as I'm aware, filter is a FireFox only feature?
no, it's everywhere except ie now...
its part of ecma5, so get used to it now.
here's a copy of filter and map for IE:
Code:
if (!Array.prototype.mArray.prototype) {// from http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:mArray.prototype
Array.prototype.mArray.prototype = function (fun) {var len = this.length;if (typeof fun != "function") {throw new TypeError;}var res = new Array(len);var thisp = arguments[1];for (var i = 0; i < len; i++) {if (i in this) {res[i] = fun.call(thisp, this[i], i, this);}}return res;};}
if (!Array.prototype.filter) { //from http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter
Array.prototype.filter = function (fun) {var len = this.length;if (typeof fun != "function") {throw new TypeError;}var res = new Array;var thisp = arguments[1];for (var i = 0; i < len; i++) {if (i in this) {var val = this[i];if (fun.call(thisp, val, i, this)) {res.push(val);}}}return res;};}
it's almost like i can't remember life before js1.6.
these two function in particular are probably the two handiest methods in the language.
the usage here is over-specific:
Code:
function checkCount(element,index,array) {
return (element.length == 3);
}
it's almost always handier later on to create a re-usable version using "this" as a stand-in:
Code:
function checkCount(element) {
return element.length == this;
}
var newTested = testedColors.filter(checkCount, 3 );
also, it can type-convert whole arrays in one call:
if you add one (or two) functions to all you scripts, make it map and/or filter.
edit:
what the heck, here's some generic functions to get everyone started:
Code:
//both filters and maps:
function extract(a){ return a[this];}
function invoke(a){ return a[this]();}
//filters
function K(a){ return a;}
function same(a){ return a==this;}
function different(a){ return a!=this;}
function equal(a){ return a===this;}
function unequal(a){ return a!==this;}
function gte(a){ return a >= this;}
function gt(a){ return a>this;}
function lte(a){ return a<=this;}
function lt(a){ return a<this;}
function isPos(a){ return a>=0;}
function isNeg(a){ return a<0;}
function contains(a){ return a.indexOf(this)>-1;}
function unique(a,b,c){return c.lastIndexOf(a)==b;}
function has(a){ return a[this] !== undefined;}
almost all of them take that 2nd argument to filter, which becomes this in the generic function.
ex:
Bookmarks