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:

  1. 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 ?
  2. 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:

  1. 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 ?
  2. 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.
Share Improve this question edited Apr 15, 2017 at 19:44 Momen Zalabany asked Apr 15, 2017 at 19:15 Momen ZalabanyMomen Zalabany 9,00716 gold badges80 silver badges144 bronze badges 5
  • 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
Add a ment  | 

1 Answer 1

Reset to default 2

is 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