Node.js:在另一个自定义 class 中引用自定义 class

Node.js: Referencing custom class within another custom class

我正在编写一个具有用户 class 的 Web 后端。它还具有记录用户统计信息的属性 class。但是,我不确定如何在 User.js 文件中引用属性 class。我希望能够或多或少地做到这一点:

function User(id){
    this.attributes = new Attributes();
}

module.exports = User;

假设您只想调用另一个模块...

var Attributes = require('/path/to/attributes.js')

function User (id) {
  this.id = id
  this.attributes = new Attributes()
}

module.exports = User

如果你想延长User

var Attributes = require('/path/to/attributes.js')

function User (id) {
  this.id = id
}

User.prototype = Object.create(Attributes.prototype);
User.prototype.constructor = Attributes;

module.exports = User

现在 Attributes 有的方法 User 都会有,你可以做 user instanceof Attributes