javascript – 获取Bookshelf.js模型时使用“new”的区别是什么?

Bookshelf.js文档的代码示例包含“new”且没有“new”:

这里http://bookshelfjs.org/#Model-instance-hasMany

let Author = bookshelf.Model.extend({
  tableName: 'authors',
  books: function() {
    return this.hasMany(Book);
  }
});

// select * from `authors` where id = 1
// select * from `books` where author_id = 1
Author.where({id: 1}).fetch({withRelated: ['books']}).then(function(author) {
  console.log(JSON.stringify(author.related('books')));
});

但这里是http://bookshelfjs.org/#Model-instance-fetch

let Book = bookshelf.Model.extend({
  tableName: 'books',
  editions: function() {
    return this.hasMany(Edition);
  },
  chapters: function{
    return this.hasMany(Chapter);
  },
  genre: function() {
    return this.belongsTo(Genre);
  }
})

new Book({'ISBN-13': '9780440180296'}).fetch({
  withRelated: [
    'genre', 'editions',
    { chapters: function(query) { query.orderBy('chapter_number'); }}
  ]
}).then(function(book) {
  console.log(book.related('genre').toJSON());
  console.log(book.related('editions').toJSON());
  console.log(book.toJSON());
});

那么区别是什么呢?

解决方法:

没有不同.

Model.fetch,Model.query,Model.where和Model.fetchAll是以下的简写:

Model[method] = function(...args) {
  return Model.forge()[method](...args);
}

而Model.forge是新手的简写.

Model.forge = function(...args) {
  return new this.constructor(...args);
}

相关文章

最后的控制台返回空数组.控制台在ids.map函数完成之前运行va...
我正在尝试将rxJava与我已经知道的内容联系起来,特别是来自J...
config.jsconstconfig={base_url_api:"https://douban....
我正在阅读MDN中的javascript,并且遇到了这个谈论承诺并且不...
config.jsconstconfig={base_url_api:"https://douban....
这是我的代码main.cpp:#include<string>#include<...