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

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

问题描述

var express = require('express'); 
var app = 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.

A 方法是附加到对象的函数。在JavaScript中,方法 主要是通过对象属性引用的函数。 (更新:自ES2015起,如果您使用方法语法创建它们,它们稍微,因为它们可以访问 super 。)

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.


它来自哪里?

Where does it come from?

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

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

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