【问题标题】:Different way of creating a Javascript Object?创建 Javascript 对象的不同方式?
【发布时间】:2013-06-17 11:21:49
【问题描述】:

我正在学习编码,我对找到的一些示例代码有疑问:

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 实例的优势吗? ({ 是什么意思?任何帮助将不胜感激

【问题讨论】:

    标签: javascript object constructor schema instance


    【解决方案1】:

    这样做的一个好处是你可以改变你的功能 不改变它的签名。另一个好处是,如果你 有很多输入参数并不是必须的,你不需要 必须为您不使用的参数添加默认值... – @Michiel Reyers

    这是一个示例,说明如何在构造函数中使用 Object Literal 作为参数创建一个新的“instance”,从而摆脱参数列表:

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

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

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

    【讨论】:

      【解决方案2】:

      在这种情况下,Schema 的构造函数的输入类型是一个对象,因此是 {} 符号。

      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 是大小的默认值。 你可以想象当有很多参数时这会很烦人。

      【讨论】:

      • 嗨,Michiel,谢谢你的回答,但我还是有点困惑,你能解释一下构造函数的输入类型是如何通俗地成为对象的吗?
      • 现在有意义了!这样做有什么好处?
      • 刚刚添加了一些关于优点的解释。
      猜你喜欢
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      相关资源
      最近更新 更多