0
如何导出不带函数的对象?ES6如何导出不带函数的对象
用户模型:
export default {
data: {},
sanitize (options) {
},
async insert (options) {
},
async find (options) {
},
async remove (options) {
}
}
用法:
const result = await user.insert({ id: '123', name: 'haha xxxx', password: 'gskgsgjs' })
console.log(user)
结果:
{ data: { id: '123', name: 'haha', _id: 59a40e73f63b17036e5ce5c4 },
sanitize: [Function: sanitize],
insert: [Function: insert],
find: [Function: find],
remove: [Function: remove] }
我什么后:
{ data: { id: '123', name: 'haha', _id: 59a40e73f63b17036e5ce5c4 }
有什么想法?
编辑:
使用ES6类:
export default class User {
constructor(options) {
this.data = this.sanitize(options)
}
sanitize (options) {
}
async insert (options) {
}
async find (options) {
}
async remove (options) {
}
}
用法:
let User = new user()
// Inject a doc.
const result = await User.insert({ id: '123', name: 'haha xxxx', password: 'gskgsgjs' })
console.log(User)
结果:
User {
data: { id: '123', name: 'haha xxxx', _id: 59a4143e63f3450e2e0c4fe4 } }
不过,并非正是我所追求的:
{ data: { id: '123', name: 'haha', _id: 59a40e73f63b17036e5ce5c4 }
什么是“insert”?为什么你需要导出时,你已经把它作为'用户'属性? – estus
@estus抱歉,结果实际上是正确的。我误解了。 – laukok
如果该方法在对象上不可用,用户应该如何调用'user.insert'? –