![](/img/trans.png)
[英]What does having brackets wrapped around the instance of constructor mean in 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
是對象的屬性。 該值是一個名為 ObjectId的函數 。 該函數具有一個屬性schemaName
,其值為'ObjectId'
。
所以輸出看起來有點奇怪,因為該函數具有自定義屬性。 那是相當罕見的。 這將是沒有自定義屬性的輸出:
{ foo: [Function: bar] }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.