[Function](包含在方括号中)在 javascript 对象内部时是什么意思?

What does a [Function] (wrapped in square brackets) mean when inside of a javascript object?

当 运行 console.log 对各种函数进行时,我将在值部分中找到值为 [Function: someFunctionName] 的对象的属性。这是什么意思?我希望能够查看函数的实际代码。当我看到此语法时,我对实际记录的内容感到困惑。

这是一个示例:日志记录 mongoose.Schema 具有以下输出

{ [Function: Schema]
  reserved: 
   { _posts: 1,
     _pres: 1,
     validate: 1,
     toObject: 1,
     set: 1,
     schema: 1,
     save: 1,
     modelName: 1,
     get: 1,
     isNew: 1,
     isModified: 1,
     init: 1,
     errors: 1,
     db: 1,
     collection: 1,
     once: 1,
     on: 1,
     emit: 1 },
  interpretAsType: [Function],
  Types: 
   { String: { [Function: SchemaString] schemaName: 'String' },
     Number: { [Function: SchemaNumber] schemaName: 'Number' },
     Boolean: { [Function: SchemaBoolean] schemaName: 'Boolean', '$conditionalHandlers': [Object] },
     DocumentArray: { [Function: DocumentArray] schemaName: 'DocumentArray' },
     Array: { [Function: SchemaArray] schemaName: 'Array' },
     Buffer: { [Function: SchemaBuffer] schemaName: 'Buffer' },
     Date: { [Function: SchemaDate] schemaName: 'Date' },
     ObjectId: { [Function: ObjectId] schemaName: 'ObjectId' },
     Mixed: { [Function: Mixed] schemaName: 'Mixed' },
     Oid: { [Function: ObjectId] schemaName: 'ObjectId' },
     Object: { [Function: Mixed] schemaName: 'Mixed' },
     Bool: { [Function: SchemaBoolean] schemaName: 'Boolean', '$conditionalHandlers': [Object] } },
  ObjectId: { [Function: ObjectId] schemaName: 'ObjectId' } }

在几行中,存在 [Function] 语法,后跟似乎是第二个 属性(即使没有中间逗号),如下所示:ObjectId: { [Function: ObjectId] schemaName: 'ObjectId' } }

那么 [Function] 实际表示的属性是什么?

我可以使用以下设置复制输出:

> function bar() {}
> bar.baz = 42;
> console.log({foo: bar});
{ foo: { [Function: bar] baz: 42 } }

所以

ObjectId: { [Function: ObjectId] schemaName: 'ObjectId' } }

表示ObjectId是一个对象的属性。该值是一个 functionname ObjectId。该函数的 属性 schemaName 值为 'ObjectId'.


所以输出看起来有点奇怪,因为该函数具有自定义属性。那是相当罕见的。这将是输出 without custom properties:

{ foo: [Function: bar] }