Jabari King
Jabari King

Reputation: 99

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

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.

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

{ [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' } }

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

Upvotes: 2

Views: 1035

Answers (1)

Felix Kling
Felix Kling

Reputation: 816462

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'.


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] }

Upvotes: 3

Related Questions