I am trying to have a object that takes in fields like this:
var User = function(params) {
if(params){
this.id = params.id;
this.name = params.name;
..
}
}
- if a property of a field in the model, it sets them
- if it does not match the model it does not get included in the model
- if the property is not there it does not set it
So if you do this:
var data = {
id: 123,
foo: 'bar'
};
var user = new User(data);
JSON.stringify(user); // { id: 123 }
What's the easiest way to do this? I can only think to do a conditional for each property.