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

244
Visualizações
Can you change console.log() output for objects?

I'm new to JavaScript, but I come from Python and Java. I know in both of those languages, there are ways to change what the console prints when the input is an object

//Java example:
public class myClass{
public toString(){
return ("This is an object!");
}
}
myClass obj = new myClass();
System.out.println(obj); //Prints "This is an object!"

However, when I looked for an example for JS, I found that the existing toString() required me to do this every time:

Console.log(obj + '')

in order to achieve the required output, otherwise it will simply print the default object text. Is there a way I can do the same thing as in Java?

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

0

I suppose you could create your own function that explicitly calls toString on its argument and logs it:

const obj = { toString() { return 'foo' } };
const myLog = obj => console.log(obj.toString());

myLog(obj);

It's not all that much, but I think it's probably the best you can do. You could also overwrite console.log with that function, but that'd be a bad idea.

I wouldn't recommend changing the default behavior. Having the object itself be logged, without side-effects (and without a non-object being logged) is what every developer who is debugging would expect.

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