什么是" Something {key:value,SomethingElse {key:value,...}}"结构体?

时间:2018-02-22 08:49:12

标签: javascript pug hexo

我正在尝试使用Hexo构建一个博客,使用Pug作为模板引擎。我do not understand如何访问全局变量(在Hexo中定义)并最终添加一些JavaScript来调试输出。

在Hexo中,site变量包含有关正在使用的站点的信息。这是一个JavaScript驱动的静态博客生成器,可以在处理过程中注入JS。我希望在处理模板时在控制台上输出site变量(_是Lodash的全局变量):

_.forEach(site, function (po) {
        console.log('post info', po)
    })

我得到的输出是(我打破了post info块):

post info Query {
  data:
   [ Document {
       title: 'first article',
       layout: 'mypost',
       _content: '\nThis is the first article',
       source: '_posts/first.md',
       raw: '---\ntitle: first article\ntags: tech\nlayout: mypost\n---\n\nThis is the first article',
       slug: 'first',
       published: true,
       date: moment("2018-02-20T20:57:27.120"),
       updated: moment("2018-02-22T09:19:24.597"),
       comments: true,
       photos: [],
       link: '',
       _id: 'cjdy8m62w00005ondnc5md94c',
       content: '<p>This is the first article</p>\n',
       site: [Object],
       excerpt: '',
       more: '<p>This is the first article</p>\n',
       path: [Getter],
       permalink: [Getter],
       full_source: [Getter],
       asset_dir: [Getter],
       tags: [Getter],
       categories: [Getter],
       prev: [Object],
       __post: true,
       lang: null,
       canonical_path: '2018/02/20/first/index.html' },
     Document {
       title: 'Hello World 2',
       _content: '\nSecond article comes here',
       source: '_posts/second.md',
       raw: '---\ntitle: Hello World 2\ntags: tech\n---\n\nSecond article comes here',
       slug: 'second',
       published: true,
       date: moment("2018-02-20T23:05:52.156"),
       updated: moment("2018-02-22T09:19:48.205"),
       comments: true,
       layout: 'post',
       photos: [],
       link: '',
       _id: 'cjdy8m63300015ond87428rhr',
       content: '<p>Second article comes here</p>\n',
       site: [Object],
       excerpt: '',
       more: '<p>Second article comes here</p>\n',
       path: [Getter],
       permalink: [Getter],
       full_source: [Getter],
       asset_dir: [Getter],
       tags: [Getter],
       categories: [Getter],
       next: [Object],
       __post: true,
       lang: null,
       canonical_path: '2018/02/20/second/index.html' } ],
  length: 2 }

post info Query { data: [], length: 0 }

post info Model {
  domain: null,
  _events: {},
  _eventsCount: 0,
  _maxListeners: undefined,
  name: 'Category',
  data: {},
  _mutex: Mutex { _locked: false, _queue: [] },
  schema:
   Schema {
     paths:
      { name: [Object],
        parent: [Object],
        slug: [Object],
        path: [Object],
        permalink: [Object],
        posts: [Object],
        length: [Object],
        _id: [Object] },
     statics: {},
     methods: {},
     hooks: { pre: [Object], post: [Object] },
     stacks:
      { getter: [Array],
        setter: [Array],
        import: [Array],
        export: [Array] } },
  length: 0,
  Document: { [Function] super_: [Function: Document] },
  Query: { [Function] super_: [Function: Query] } }

post info Model {
  domain: null,
  _events: {},
  _eventsCount: 0,
  _maxListeners: undefined,
  name: 'Tag',
  data:
   { cjdy8m63600025ondq69zill9: { name: 'tech', _id: 'cjdy8m63600025ondq69zill9' } },
  _mutex: Mutex { _locked: false, _queue: [] },
  schema:
   Schema {
     paths:
      { name: [Object],
        slug: [Object],
        path: [Object],
        permalink: [Object],
        posts: [Object],
        length: [Object],
        _id: [Object] },
     statics: {},
     methods: {},
     hooks: { pre: [Object], post: [Object] },
     stacks:
      { getter: [Array],
        setter: [Array],
        import: [Array],
        export: [Array] } },
  length: 1,
  Document: { [Function] super_: [Function: Document] },
  Query: { [Function] super_: [Function: Query] } }

post info {}

我对QueryModelSchemaDocument是什么以及如何访问其中的数据感到茫然。

它们中的任何一个似乎都是一个Object(键和值用冒号分隔),但它也包含诸如

之类的结构
schema:
 Schema {
   paths:
    { name: [Object],
    (...)

我不明白(Schema {部分,具体而言)。

这是什么类型的数据结构以及如何访问其内容?

1 个答案:

答案 0 :(得分:1)

这些实体是Javascript classes 当一个类实例被记录到浏览器控制台时,它的类名被保留,这是为什么好的命名通常对调试非常有用的一部分。请参阅以下示例:

class Foo {
  // Define methods and properties
}
const instance = new Foo();
// Will log `{}` in the snippet console, but `Foo {}` in the browser console.
console.log(instance);