Is this the correct way to make a javascript class with default properties (even though some here are null), yet also having the option to add a creation argument that can contain specific values for some properties listed in object format/json?
Also should i put any of this into Person’s prototype to save memory if I am creating many Person objects?
this is working for me but I wonder if it is a good way to do this ?
// PERSON Class -----------------------------------------------
QM.Person=function(data)
{
/* Closure for this */
var my = this;
this.PersonID=null;
this.Name_Last="";
this.Name_First="";
this.Date_Birth=null;
this.Biography="";
this.loadData=function(obj) {
for (var i in obj) {
if (my.hasOwnProperty(i)) {
my[i] = obj[i];
}
}
}
this.loadData(data);
}
example of creating using this class:
jondoe = new Person();
bob = new Person({Name_First:"Bob",Name_Last:"Bar"});
jondoe.Name_First /* "" */
bob.Name_First /* "Bob" */
To avoid repeating yourself you could build a list of keys you expect and also the default values to insert if they aren’t specified: