var express = require('express');var app = express(), express() 是什么??它是一个方法还是一个构造函数?它从何而来 [英] var express = require('express'); var app = express(), What is express()?? is it a method or a constructor? Where does it come from

查看:41
本文介绍了var express = require('express');var app = express(), express() 是什么??它是一个方法还是一个构造函数?它从何而来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var express = require('express'); 
var app = express();

这就是我们创建快速应用程序的方式.但是这个'express()'是什么?它是一个方法还是一个构造函数?它来自哪里??

This is how we create an express application. But what is this 'express()'? Is it a method or a constructor? Where does it come from??

推荐答案

是方法还是构造函数?

Is it a method or a constructor?

都不是;这是一个函数,虽然如果你说方法"我认为没有人会给你带来困难.

Neither; it's a function, although if you said "method" I don't think anyone would give you a hard time.

方法 是附加到对象的函数.在 JavaScript 中,方法只是主要是您通过对象属性引用的函数.(更新:从 ES2015 开始,如果您使用方法语法来创建它们,它们会稍微多一点,因为它们可以访问 super.)em>

A method is a function attached to an object. In JavaScript, methods are just mostly functions that you reference via object properties. (Update: As of ES2015, if you use method syntax to create them, they're slightly more than that because they have access to super.)

构造函数,在 JavaScript 中,是您通过 new 运算符调用的函数.尽管其他函数可能会创建事物,但我们通常不会称它们为构造函数".以免混淆.有时他们可能是创造者"或建造者"功能.

A constructor, in JavaScript, is a function you call via the new operator. Even though other functions may create things, we don't typically call them "constructors" to avoid confusion. Sometimes they may be "creator" or "builder" functions.

它来自哪里?

ExpressJS 是一个 NodeJS 模块express 是模块的名称,也是我们通常赋予变量的名称,用于在代码中引用其主要功能,例如您引用的内容.NodeJS 提供了require 函数,其作用是加载模块并让您访问它们的导出.(你没有调用变量express,你可以做var foo = require('express');并使用foo 代替,但约定是您将使用模块的名称,或者如果仅使用模块的一部分,则使用模块文档定义的该部分的名称.)

ExpressJS is a NodeJS module; express is the name of the module, and also the name we typically give to the variable we use to refer to its main function in code such as what you quoted. NodeJS provides the require function, whose job is to load modules and give you access to their exports. (You don't have to call the variable express, you can do var foo = require('express'); and use foo instead, but convention is that you'd use the module's name, or if only using one part of a module, to use the name of that part as defined by the module's documentation.)

express 的默认导出有点不寻常,因为它是一个函数,上面也有属性,这些属性也是函数(方法).这在 JavaScript 中完全有效¹,但在其他一些语言中却相当不寻常.这就是为什么你可以通过 express() 创建一个 Application 对象,但也可以使用 express.static(/*...*/)设置提供静态文件.

The default export of express is a bit unusual in that it's a function that also has properties on it that are also functions (methods). That's perfectly valid in JavaScript,¹ but fairly unusual in some other languages. That's why you can create an Application object via express(), but also use express.static(/*...*/) to set up serving static files.

¹ 事实上,这是完全正常的.默认情况下,函数有几个标准方法:例如 callapplytoString.

¹ In fact, it's completely normal. Functions have a couple of standard methods by default: call, apply, and toString for instance.

这篇关于var express = require('express');var app = express(), express() 是什么??它是一个方法还是一个构造函数?它从何而来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆