locked
Javascript functions (goto: references)

    Question

  • So i have been learning JS/HTML5 to make some simple metro apps and i have come across areas of code that appear to be marked with goto: syntax (for lack of a better term), but they can be called as a function. Could someone please clear up what this syntax means and what it is called so i can google it.

    Here is an example:

    Here to goto "createFiltered:" is declared, and it clearly points to what appears to be a function with no name.

    createFiltered: function (predicate) {
            // Lots of code in here
            return someValue;
    },


    Here the goto statment is called as though it was a function:

    return list.createFiltered(function (item) { return item.group.key === group.key; });

    Wednesday, August 01, 2012 4:15 PM

Answers

  • This is just simple object notation. In this instance they're declaring a "list" object, and a "createFiltered" member that happens to be a function. Take a look at this example:

    // object notation. Curly brace with a comma separated list of key value pairs.
    var myObject = {
        aFunction: function (arg) { 
            return this.aNumber + arg; 
        },
        aNumber: 42,
        aString: "Hello",
        anArray: ["zero", "one", "two"]
    };
    
    // This object's properties can now be accessed like so:
    var result = myObject.aFunction(10);
    
    result === 53; // true
    myObject.anArray[1] === "one"; // true
    
    // You can also access object members like a hashtable
    myObject.aString === myObject["aString"]; // true
    
    // You can also define object members in an ad hoc fashion.
    myObject.anotherFunction = function(arg) { 
        return arg;
    };
    
    "test" === myObject.anotherFunction("test"); // true

    Hope that helps!

    Here's a free online book on JavaScript:

    http://eloquentjavascript.net/


    Wednesday, August 01, 2012 4:52 PM

All replies

  • This is just simple object notation. In this instance they're declaring a "list" object, and a "createFiltered" member that happens to be a function. Take a look at this example:

    // object notation. Curly brace with a comma separated list of key value pairs.
    var myObject = {
        aFunction: function (arg) { 
            return this.aNumber + arg; 
        },
        aNumber: 42,
        aString: "Hello",
        anArray: ["zero", "one", "two"]
    };
    
    // This object's properties can now be accessed like so:
    var result = myObject.aFunction(10);
    
    result === 53; // true
    myObject.anArray[1] === "one"; // true
    
    // You can also access object members like a hashtable
    myObject.aString === myObject["aString"]; // true
    
    // You can also define object members in an ad hoc fashion.
    myObject.anotherFunction = function(arg) { 
        return arg;
    };
    
    "test" === myObject.anotherFunction("test"); // true

    Hope that helps!

    Here's a free online book on JavaScript:

    http://eloquentjavascript.net/


    Wednesday, August 01, 2012 4:52 PM
  • Thanks a lot, i don't think i could have asked for a simpler well defined answer.

    But shouldn't

    result === 53; // true

    be 52?

    • Edited by ResQue1980 Wednesday, August 01, 2012 5:32 PM
    Wednesday, August 01, 2012 5:30 PM