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

241
Visualizações
Javascript: concatenate function with the string

Following the question here:

JavaScript getting filename without extension located in different folder

I would like to concatenate this function with my string.

When I apply this code

"use strict";
window.addEventListener('load', onLoad, false);

function onLoad(evt) {
  let scripts = document.querySelectorAll('script');
  scripts.forEach(scr => {
    if (scr.src != '') {
      let slashPos = scr.src.lastIndexOf('/');
      let filename = scr.src.slice(slashPos + 1);
      filename = filename.replace('.js', '');
      //console.log(filename);
      return filename
    }
  });
}

var string = 'Date = ' + onLoad;

console.log(string)My console shows the full function instead of a certain date.

enter image description here

what should I do to have the date populated properly?

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

0

You are not calling the function onLoad because there are no parenthesis, you are passing a reference to it which is your entire function.

var string = 'Date = ' + onLoad();

about 4 years ago · Juan Pablo Isaza Relatório

0

You are not running your function. You are doing something akin:

function return_string() {
 return 'a string';
}

var string = 'Date = ' + return_string;
console.log( string );

That returns the function, not the result. So just execute the function as normal

function return_string() {
 return 'a string';
}

var string = 'Date = ' + return_string();
console.log( string );

about 4 years ago · Juan Pablo Isaza Relatório

0

This is happening because in javascript if you just call the function without braces it returns the function definition instead of actually invoking the function. So, calling onload like this would return its' function definition. You should call onload() to get the actual return value of the function. But then I saw there's no return from that function as well. So what are you expecting from this function to be returned? If you want to get Date inside JavaScript you should call the Date constructor like this new Date() to get the current Date. For more information on the Date constructor visit mdn.

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