2013-06-20 53 views
0

我学习到的代码和我有一个关于一些示例代码,我发现一个问题:创建Javascript对象的不同方式?

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

据我了解OOP,我想到了SchemaComment对象实例就这样可以了:

function Schema (user, time, content){ 

    this.user = user; 
    this.time = time; 
    this.content = content; 
}; 

var Comment = new Schema (userStub, time, content); 

任何人都知道通过var Comment = new Schema({建立Comment实例的好处吗? ({意味着什么?任何帮助将不胜感激

回答

0

架构的构造函数的输入类型在这种情况下是一个对象,因此{}表示法。

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

对象只是一个变量,就像一个字符串或数字。所以你可以将它传递给一个函数。但是,而不是首先创建一个对象,在你的榜样输入对象是用这样

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

通话,而不是:

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

这样做的好处是,你可以改变你的功能不会改变它的签名。另一个优点是,如果您有许多输入参数并非都是强制性的,那么您不必为未使用的参数添加默认值。 例如:

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

如果函数签名是:

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

你将不得不拨打:

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

其中0是大小的默认值。 你可以想象,当有很多参数时,这可能很烦人。

+0

嗨米歇尔感谢您的回答,但我还是有点糊涂了,你可以请解释构造函数的输入类型如何成为外行人的对象? –

+0

现在有道理!这样做的好处是什么? –

+0

只是增加了一些关于优点的解释。 – Michiel

1

做这种方式的好处是,你可以改变你的函数 没有量变到质变它的签名。另一个优点是,如果你 有大量的输入参数与不都是强制性的,你不 必须添加默认值,你不使用参数... – @Michiel Reyers

下面是如何创建一个新的“实例”对象文本如在构造函数中的参数,除暴安良的参数列表中的一个例子:

function Schema (options) { 
    "use strict"; 
    options = options || {}; 
    this.user = options.user || "unnamed"; 
    this.time = options.time || null; 
    this.content = options.content || {}; 
} 

在前面的方法中,我们可以通过在入口参数中指定none,一个或所有属性来创建新对象。除了构造方法的签名保持不变:

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

您也可以扩展对象,这样的构造函数可以通过在输入参数延伸到该实例的新属性更加灵活。在这个例子中,我将使用jQuery(我知道,不在标签中),但是您可以创建一个自定义方法来扩展没有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; 
}()); 

这里我们使用了构造模式。您可以使用Schema并添加新属性,而不必修改构造函数的签名。 (另见MODULE模式)。

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

以前的方法的改进,是设置在构造函数原型的默认值:

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