An advantage of doing it this way is that you can change your function
without changeing it's signature. Another advantage is that if you
have a lot of input parameters with are not all obligatory, you don't
have to add default values for parameters you're not using... – @Michiel Reyers
Here is an example on how you can create a new "instance" with an Object Literal as parameter in the constructor function, getting rid of the list of parameters:
function Schema (options) {
"use strict";
options = options || {};
this.user = options.user || "unnamed";
this.time = options.time || null;
this.content = options.content || {};
}
In the previous approach, we can create new objects by specifying none, one or all the properties in the entry parameter. Besides the signature of the constructor keeps unchanged:
var comment;
//no arguments
comment = new Schema();
//only one option
comment = new Schema({ user: "luis" });
//multiple options
comment = new Schema({
user: "Federico",
time: new Date(1995, 10 - 1, 31), //october 31, 1995
content: { type: Types.String, required: true, trim: true }
});
Also you can extend the object, so the constructor function can be more flexible by extending into the instance the new properties in the entry parameter. For this example I'm going to use jQuery (I know, is not in the tag), but you can create a custom method to extend objects without jQuery.
//pointer to the internal Schema
var Schema = (function () {
//cached default values
var defaults = {
user: "unnamed",
time: null,
content: {}
};
//constructor extensible
function Schema (options) {
"use strict";
//merges @options and @defaults into the instance @this
jQuery.extend(this, defaults, options);
}
//returns the reference to Schema;
return Schema;
}());
Here we used the Constructor pattern. You can use the Schema
and add new properties without having to modify the signature of the constructor. (Also see MODULE pattern).
var comment = new Schema({ user: "Felipe", age: 31 });
An improvement to the previous approach, is to set the default values in the constructor prototype:
//pointer to the internal Schema
var Schema = (function ($) {
//constructor extensible
function Schema (options) {
"use strict";
//merges @options into the instance @this
$.extend(this, options);
}
//sets the default values
Schema.prototype = {
"user": "unnamed",
"time": null,
"content": {}
};
//returns the reference to Schema;
return Schema;
}(jQuery));
var comment = new Schema();
console.log(comment);
comment = new Schema({ user: "coco", age: +"d" });
console.log(comment);