Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!