views:

117

answers:

3
//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!!!

A: 

This is a lambda expression like.

sort.serialize()

accept the function as parameter, not the value.

J-16 SDiZ
A: 

The first code is probably correct.

In JavaScript, functions are stored in variables just as any other value (as you see with serializeFunction), and sort.serialize only takes a reference to serializeFunction. Then serializeFunction is called from sort.serialize with the current element (el).

The second code would send an undefined value to the serializeFunction (since el has not been defined in that scope) which would throw an error. Even if el was defined, sort.serialize expects a reference to a function, not a value.

Blixt
+1  A: 

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.

CMS
If I had to invoke serializeFunction, would I need to pass el to it?
Delirium tremens
I think that understanding how JavaScript Closures work (http://is.gd/1dUUH), will clear all your doubts.
CMS
Yes, you should invoke the function with the value of the el parameter: serializeFunction(myEl);
CMS