简体   繁体   English

这是什么类型的声明? 功能? 类?

[英]What type of declaration is this? a function? class?

I was looking at code in node.js, express, mongoose. 我当时在看node.js中的代码,表达出来,猫鼬。 It is defining a schema: 它正在定义一个模式:

var Comment = new Schema({
    username : String,
    content  : String,
    created  : Date
});

I do not understand what this actaully is, because 我不明白这是什么,因为

var Comment = 

Is like a function/variable 就像一个函数/变量

new Schema

is like a class and 就像一个班级

    {
        username : String,
        content  : String,
        created  : Date
    }

is like an associative array... 就像一个关联数组...

What exactly is this type of block? 这类块到底是什么? and how does it work? 以及它如何运作?

Thanks a lot 非常感谢

This is the object-oriented style of JavaScript, especially the concept of model-view controller. 这是JavaScript的面向对象样式,尤其是模型视图控制器的概念。

What you describe in your examples here is called a model. 您在示例中描述的内容称为模型。 It defines how the data which are put in and out fit into the concept. 它定义了输入和输出的数据如何适合该概念。

So this model would be named comment and it would hold username, content as strings and created as date. 因此,该模型将被命名为comment,并将包含用户名,内容作为字符串并创建为日期。 It would be easily saveable to a database later by calling eg 稍后可以通过调用例如将其轻松保存到数据库中

Comment.save();

"new Schema" tells the engine here that a new model should be created. “新模式”在这里告诉引擎应该创建一个新模型。

Please see also here: http://mongoosejs.com/docs/guide.html 另请参见此处: http : //mongoosejs.com/docs/guide.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM