• Jobs
  • About Us
  • Jobs
    • Home
    • Jobs
    • Courses and challenges
  • Businesses
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

213
Views
How does function invocation work in JavaScript as an value to part of an object?

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?

over 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This would work because it passes the value of the property to your function:

{
    "data": "Date",
    "autoWidth": true,
    "render": value => GetDateTime(value)
}
over 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Show me some job opportunities
There's an error!