Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
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?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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

{
    "data": "Date",
    "autoWidth": true,
    "render": value => GetDateTime(value)
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda