为什么当我们控制台记录一个函数时返回 "fucntion body" 而不是 "function object"?

why "fucntion body" is returned instead of "function object" when we console log a function?

我有一个对象“person1”,其中有一个方法“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);

我的问题是为什么on console.log(person1.enroll)返回function body or function definition(ƒ abc() {console.log("hello world")}),为什么整个函数对象不是这样返回=>

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

以及为什么我必须执行 console.dir(person1.enroll) 才能查看 enroll 函数对象的所有属性和方法。为什么 console.log(person1.enroll) 不授予对注册函数内所有方法和属性的访问权。

为什么 log 产生它所做的和 dir 产生它所做的,答案是它们是出于特定原因而编写的。根据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."

因此,log 没有为您提供所有属性和方法等信息。 log 仅用于“一般日志记录”,为您提供有关该对象的一些提示。我们有 dir 用于输出所有内容的明确目的。换句话说,log 没有给你详细的描述,因为控制台对象的设计者选择将这些信息放在 dir.

(设计者 可以 选择使用 log 输出更多,但他们想为 dir 保留更强大的功能。此外,您可能熟悉 out console dot logging general object 给出了通常无用的 [object Object] 所以记录一个函数也不提供完整的细节也就不足为奇了。)