在[javascript]对象内部,[Function](用方括号括起来)是什么意思?

时间:2022-02-06 16:56:04

When running console.log on various functions, I'll find properties on the object that have a value of [Function: someFunctionName] in the value section. What does this mean? I want to be able to view the actual code of the function. I'm confused on what's actually being logged when I see this syntax.

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

This was an example: Logging mongoose.Schema had the following output

这是一个例子:记录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' } }

On several lines, there's the [Function] syntax present followed by what seemed to be a second property (even though there was no intermediate comma) as seen here: ObjectId: { [Function: ObjectId] schemaName: 'ObjectId' } }

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

So what are those properties with the [Function] actually denoting?

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

1 个解决方案

#1


2  

I'm able to replicate the output with the following setup:

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

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

So

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

means that ObjectId is the property of an object. The value is a function with name ObjectId. That function has a property schemaName with value 'ObjectId'.

表示ObjectId是对象的属性。该值是名为ObjectId的函数。该函数具有值为'ObjectId'的属性schemaName。


So the output only looks a bit strange because the function has custom properties. That's rather rare. This would be the output without custom properties:

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

{ foo: [Function: bar] }

#1


2  

I'm able to replicate the output with the following setup:

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

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

So

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

means that ObjectId is the property of an object. The value is a function with name ObjectId. That function has a property schemaName with value 'ObjectId'.

表示ObjectId是对象的属性。该值是名为ObjectId的函数。该函数具有值为'ObjectId'的属性schemaName。


So the output only looks a bit strange because the function has custom properties. That's rather rare. This would be the output without custom properties:

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

{ foo: [Function: bar] }