这是我的模特课:
'use strict'
import MongoDb from 'database/mongodb'
export default class Model {
constructor (options) {
this.data = this.sanitize(options)
}
objectId (_id) {
// https://stackoverflow.com/questions/17545311/correct-way-to-search-for-mongodb-entries-by-id-in-node
const mongodb = new MongoDb()
const objectId = mongodb.objectId
return objectId(_id)
}
async db () {
const mongodb = new MongoDb()
const connection = await mongodb.connect()
return connection
}
sanitize (options) {
return options
}
async insert (options) {
//
}
async find (options) {
//
}
async remove (options) {
//
}
}
我有一个用户类扩展它:
'use strict'
import lodash from 'lodash'
import Model from 'model'
import schemas from './schemas'
export default class User extends Model {
constructor (options) {
super(options)
}
sanitize (options) {
let data = options || {}
let schema = schemas.user
let keys = lodash.keys(schema)
let defaults = lodash.defaults(data, schema)
let picked = lodash.pick(defaults, keys)
return picked
}
...
...
}
但根据standardjs:
standard: Use JavaScript Standard Style (https://standardjs.com)
/var/www/html/.../user/models/user.js:8:3: Useless constructor.
为什么我的constructor
是Useless
?
根据我的理解,我必须在我的扩展课程中使用super()
,即使我还没有完全理解它。我用错了吗?
有什么想法吗?
修改
如果我忽略扩展类中的constructor
,我会收到此错误:
Syntax Error: missing super() call in constructor
答案 0 :(得分:3)
constructor (...args) {
super(...args);
}
当省略constructor
时,将是扩展类中的默认构造函数。这就是为什么它被标记为无用的'。
只有在super
时才应在扩展类中指定 constructor
,并且只有在除了调用具有完全相同参数的父构造函数之外还应该存在某些逻辑时才应指定constructor
。 / p>