admin管理员组文章数量:1323714
i wonder how to create models in javascript ?
example Object user: should have these properties [name,username,password]
, and no other properties should be there.
now i want to send in any value it should return an objects with these 3 properties and ignore any other props.
i tried using
var UserFactory = props => ({
user :props.user||'',
username:props.username||'',
..etc
})
now when ever i pass user object arround im sure all properties does exists and no undefined error may occur.
reason i want this is to normalize data when fetching/posting to server.
is there an already best practice to how to do this ?
p.s: if it matters, i'm using this in a react-redux learning project ..
thanks
Edit problem with code above:
- i cannot do type check because what factory returns is a plain object not instance of a model
userFactory({}) instanceOf UserObject === false
so how to make sure if a variable is holding a userObject inside ? - its verbose, yet if i use Object.assign(), i might get unwanted properties in my object, so i'm not sure if this is best way to do it.
i wonder how to create models in javascript ?
example Object user: should have these properties [name,username,password]
, and no other properties should be there.
now i want to send in any value it should return an objects with these 3 properties and ignore any other props.
i tried using
var UserFactory = props => ({
user :props.user||'',
username:props.username||'',
..etc
})
now when ever i pass user object arround im sure all properties does exists and no undefined error may occur.
reason i want this is to normalize data when fetching/posting to server.
is there an already best practice to how to do this ?
p.s: if it matters, i'm using this in a react-redux learning project ..
thanks
Edit problem with code above:
- i cannot do type check because what factory returns is a plain object not instance of a model
userFactory({}) instanceOf UserObject === false
so how to make sure if a variable is holding a userObject inside ? - its verbose, yet if i use Object.assign(), i might get unwanted properties in my object, so i'm not sure if this is best way to do it.
- What is wrong with what code you posted? – Err Commented Apr 15, 2017 at 19:22
- what problems are you actually encountering currently...provide examples – charlietfl Commented Apr 15, 2017 at 19:26
- @charlietfl provided an example, please check edit – Momen Zalabany Commented Apr 15, 2017 at 19:45
- doesn't explain where extra properties e into play – charlietfl Commented Apr 15, 2017 at 19:50
-
@charlietfl
userFactory({badProperty:'i dont need you',fullname:'i want this',username:'and this'})
– Momen Zalabany Commented Apr 15, 2017 at 19:51
1 Answer
Reset to default 2is there an already best practice to how to do this ?
Yes, and this is what you are doing - using object factories. It is a mon approach in JS world when constructing new models. However, instead of using ES5 approach use ES6 approach of parameter handling:
let UserFactory = ({user='', username='', etc...}) => { ... }
See more here.
本文标签: how to create a model in javascript to make sure all properties does existsStack Overflow
版权声明:本文标题:how to create a model in javascript to make sure all properties does exists - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742128358a2422045.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论