I'm working with datatables.js library. The contents of the code isn't what I'm trying to understand, but how functions work as arguments/parameters. Why doesn't the last version work, whereas the first two do? I understand that the first two are essentially the same thing, but what about javascript prevents me from do the last option below?
function GetDateTime(value){
if (value == null) return "";
var pattern = /Date\(([^)]+)\)/; //date format from server side
var results = pattern.exec(value);
var dt = new Date(parseFloat(results[1]));
var tMonth = dt.getMonth() + 1;
var tDay = dt.getDate();
return (tMonth.toString().length > 1 ? tMonth : "0" + tMonth) + "/" + (tDay.toString().length > 1 ? tDay : "0" + tDay) + "/" + dt.getFullYear();
} // function GetDateTime
...
// This works
{
"data": "Date",
"autoWidth": true,
"render": function (value) {
return GetDateTime(value);
}
}
...
// This works
{
"data": "Date",
"autoWidth": true,
"render": function (value) {
// The exact same code in GetDateTime(value)
}
}
...
// This does not work
{
"data": "Date",
"autoWidth": true,
"render": GetDateTime(value)
}
}
As far as I'm aware, the last one is doing the exact same thing, however when I do that, it throws an error saying "value is undefined." How is value any more defined than with the anonymous function declaration? What about that causes javascript to pass a value in there as opposed to inside an implicit function call?
This would work because it passes the value of the property to your function:
{
"data": "Date",
"autoWidth": true,
"render": value => GetDateTime(value)
}