I am currently converting an Object constructor from the form of
function Obj(arg1, arg2, arg3, arg4, ..., argN-1, argN) {
this.name = arg1;
this.field = arg2;
this.etc = arg3;
...
} //Obj
to the form of
function Obj() {
var args = Array.prototype.slice.call(arguments);
var defaultArgs = {
'field' : default
...
};
... check args, etc
} //Obj
Here is my code so far:
function DSObj () {
var args = Array.prototype.slice.call(arguments);
var defaultArgs = {
'fields' : values or functions returning values
...
};
var options = {};
for (var i in defaultArgs) {
if (typeof(args[i] == 'undefined')) {
options[i] = defaultArgs[i];
} else {
options[i] = args[i];
} //check if the user supplied this argument
} //for each possible argument
for (var name in options) {
this.name = options[name]; //trying to make fields - obviously wrong
} //for each final parameter
} //constructor for DSObj
So, what I need help with is the last part of the above constructor. I just started programming JavaScript around a week ago, and my education goes about as far as reading a beginners syntax book. So, if there is a better way to do this other than the stupid and direct way, please let me know. The reason that I can't just reference the options array above is that all of the methods written for this object in some way use the "this.method" paradigm. Please. Guise. Help.