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

234
Visualizações
why "fucntion body" is returned instead of "function object" when we console log a function?

I have an object "person1" and in it, there is a method "enroll" ==>

      function Person(first, last, age, gender) {
        this.enroll = function abc() {
            console.log("hello world")
        }
        // property and method definitions
        this.name = {
          first: first,
          last: last,
        };
        this.age = age;
        this.gender = gender;
        //...see link in summary above for full definition
      }

      let person1 = new Person('Bob', 'Smith', 32, 'male', ['music', 'skiing']);

      console.log(person1);

My question is why on console.log(person1.enroll) function body or function definition(ƒ abc() {console.log("hello world")}) is returned, why the whole function object is not returned like this =>

ƒ abc()
    arguments: null
    caller: null
    length: 0
    name: "abc"
    prototype: {constructor: ƒ}
    [[FunctionLocation]]: oop2.html:12
    [[Prototype]]: ƒ ()
    [[Scopes]]: Scopes[2]

and why do I have to do console.dir(person1.enroll) to see all properties and methods of enroll function object. Why console.log(person1.enroll) does not give access to all methods and properties inside of enroll function.

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

0

The answer to why does log produce what it does and dir produce what it does is that they are written for a particular reason. According to the documentation

The intent behind log is "for general output of logging information"

The intent behind dir is to "display an interactive listing of the properties of a specified JavaScript object. This listing lets you use disclosure triangles to examine the contents of child objects."

So by intent, log does not give you all the properties and methods and other information. log is just for "general logging," to give you some hint about the object. We have dir for the express purpose of outputting everything. In other words log doesn't give you a detailed description because the designers of the console object chose to put that information in dir.

(The designers could have chosen to output more with log but they wanted to reserve that more powerful feature for dir. Besides you are probably familiar with out console dot logging general object gives the often useless [object Object] so it should not really be a surprise that logging a function does not give full details either.)

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