Search code examples
javascriptparametersmootoolsanonymous-function

Advanced parameter usage


//This is the function that will run every time a new item is added or the 
//list is sorted.
var showNewOrder = function() {
    //This function means we get serialize() to tell us the text of each 
    //element, instead of its ID, which is the default return.
    var serializeFunction = function(el) { return el.get('text'); };
    //We pass our custom function to serialize();
    var orderTxt = sort.serialize(serializeFunction);
    //And then we add that text to our page so everyone can see it.
    $('data').set('text', orderTxt.join(' '));
};

full code is at http://demos.mootools.net/Dynamic.Sortables

var serializeFunction = function(*el*) { return el.get('text'); };
var orderTxt = sort.serialize(serializeFunction*(el)*);

compare the codes.

Is el being passed or not? what is going on???

I want to learn advanced parameter usage.

If not declaring functions like function name(parameter1, parameter2, parameter3...). If not calling functions like name(parameter1, parameter2, parameter3...). If parameters aren't variables.

If declaring functions like function(parameter1, parameter2, parameter3...). If calling functions like variable(parameter1, parameter2, parameter3...). If parameters are objects.

I'm interested.

You probably have a bookmark with the lessons in which I'm interested... please, share!!!


Solution

  • The value assigned to "serializeFunction" is actually an anonymous function, you can see it like a pointer or reference to a function, "el" is simply a declared input parameter that will be used then that function will be called.

    Looking at the original code of the one that was posted, the call of the sort.serialize function, receives only the function as a parameter, the "serializeFunction" is not being invocated, it's only passed as an argument.

    So, the serialize function that receives the reference of the function passed as a parameter it will be in charge of execute it internally.