Results 1 to 3 of 3

Thread: Calling a function without parameters?

  1. #1
    Join Date
    Oct 2009

    Calling a function without parameters?

    Hi guys,

    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 />");
    <body onload="testingCallbacks();">

  2. #2
    Join Date
    Aug 2009
    Quote Originally Posted by nabeca View Post
    testedColors[i] = colors[i].filter(checkColor);
    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?
    Last edited by thraddash; 10-22-2009 at 03:36 AM.

  3. #3
    Join Date
    Jul 2008
    urbana, il
    Quote Originally Posted by thraddash View Post
    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:
    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:
    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:

    function checkCount(element) {
      return element.length == this;
    var newTested = testedColors.filter(checkCount, 3 );
    also, it can type-convert whole arrays in one call:

    [1,2,3, false, "fred" ].map(String)//===["1", "2", "3", "false", "fred"]
    "1,2,3".split(",").map(Number)//===[1, 2, 3]
    and it makes for simple but handy type filters as well:

    [3,60,"fred",5,undefined,8,NaN,4,""].filter(Number) //===[3, 60, 5, 8, 4]
    if you add one (or two) functions to all you scripts, make it map and/or filter.

    what the heck, here's some generic functions to get everyone started:

    //both filters and maps:
    function extract(a){ return a[this];}
    function invoke(a){ return a[this]();}
    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.
    [1,2,3,4].map(String).map(invoke, "bold")//===["<b>1</b>", "<b>2</b>", "<b>3</b>", "<b>4</b>"]
    pretty cool huh?
    Last edited by rnd me; 10-22-2009 at 04:35 AM.
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center