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'? –