Different way to create a Javascript object?

I am studying the code, and I have a question about some sample code that I found:

var Comment = new Schema({ user:userStub, time:Date, content: {type:String, required: true, trim:true} }); 

From what I learned about OOP, I thought that an instance of the Comment object of the Schema object would be constructed as follows:

 function Schema (user, time, content){ this.user = user; this.time = time; this.content = content; }; var Comment = new Schema (userStub, time, content); 

Does anyone know the benefit of creating a Comment instance through var Comment = new Schema({ instead? What does it mean ({ ? Any help would be greatly appreciated

0
javascript object constructor instance schema
Jun 20 '13 at 2:31 on
source share
2 answers

The advantage of this is that you can change your function without changing its signature. Another advantage is that if you have many input parameters, not all are mandatory, you should not add default values ​​for parameters that you do not use ... - @Michiel Reyers

Here is an example of how you can create a new “instance” with the Object Literal parameter in the constructor function, getting rid of the parameter list:

 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, none, or all properties in the input parameter. In addition, the signature of the constructor does not change:

 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 } }); 

You can also expand the object, so the constructor function can be more flexible by adding new properties to the instance in the input parameter. In this example, I will use jQuery (I know what is not in the tag), but you can create your own method for expanding 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 template. You can use Schema and add new properties without having to change the constructor signature. (Also see MODULE template ).

 var comment = new Schema({ user: "Felipe", age: 31 }); 

An improvement on the previous approach is to set the default values ​​in the prototype constructor:

 //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); 
+1
Oct 31 '13 at 18:50
source share

The input type of the constuctor function of the circuit in this case is an object, therefore, the notation {}.

 function Schema (o){ this.user = o.user; this.time = o.time; this.content = o.content; }; 

An object is just a variable, like a string or a number. That way you can pass it to a function. But instead of creating an object first, the input object in your example is written in a call like this

 mySchema = new Schema({user:'john'}); 

instead:

 var myObj = {user:'john'}; mySchema = new Schema(myObj); 

The advantage of this method is that you can change your function without changing its signature. Another advantage is that if you have many input parameters, not all of them are required, you do not need to add default values ​​for parameters that you do not use. For example:

 var mySchema1 = new Schema({size:25}); var mySchema2 = new Schema({name:'the best schema ever'}); 

if the function signature is:

 function Schema(size,name) { // code here } 

You will need to call:

 var mySchema2 = new Schema(0,'the best schema ever'); 

Where 0 is the default value for the size. You can imagine that it can be annoying when there are many options.

0
Jun 20 '13 at 14:35
source share



All Articles