当我需要“表达"时我需要知道它是对象还是函数 [英] when I require "express" I need to know if its an object or function

查看:74
本文介绍了当我需要“表达"时我需要知道它是对象还是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出我的代码

const express = require("express")
const app = express()


console.log(typeof app)
console.log(typeof express)

我的终端告诉我这两个变量都是函数!

My terminal tells me that both of these variables are functions!

这对我来说毫无意义,因为我可以像对待对象一样访问它们中的每个属性,但是当我在console.log中执行typeof时,终端会告诉我它们都是函数.

This makes no sense to me because I can access properties from each of them as if they were objects and yet when I do a typeof in console.log my terminal tells me they are both functions.

有人可以向我解释这些变量是什么,以及为什么有可能像访问对象一样访问它们的属性吗?

Can someone please explain to me what these variables are and why its possible to access their properties as if they were objects?

推荐答案

当我需要表达"时,我需要知道它是对象还是函数

when I require "express" I need to know if its an object or function

都是. Javascript中的函数是对象.

It's both. Functions in Javascript are objects.

在不掌握技术的情况下,express是工厂功能,可创建应用程序实例,而app是快速实例.现在,从技术上讲,expressapp都是函数.但是,JavaScript中的函数是对象,并且可以具有属性. express对象具有静态属性. app对象的行为非常类似于express类的实例.它同时具有方法和实例数据.

Without getting technical, express is a factory function that creates an app instance and app is an express instance. Now, technically, both express and app are functions. But, functions in Javascript are objects and can have properties. The express object has static properties. The app object behaves much like an instance of the express class. It has both methods and instance data.

现在,要多一点技术...

Now, getting a little more technical...

const express = require('express')为您提供了一个函数,该函数也是一个对象并具有属性.

const express = require('express') gets you a function and that function is also an object and has properties.

此特定功能是工厂功能,在调用它时会创建一个app对象,该对象也是具有属性的功能.

This specific function is a factory function that creates an app object when you call it that is also a function that has properties.

因此,express也具有可以使用的属性,例如:

So, express also has properties that can be used such as:

express.static
express.json
express.urlencoded

而且,可以这样称呼它:

And, it can be called as in:

const app = express();

同样,app是一个也具有属性的函数.可以用作以下功能:

Likewise, app is a function that has properties too. It can be used as a function as in:

const server = http.createServer(app);
server.listen(80);

或者,它可以像对象一样使用:

Or, it can be used like an object as in:

const server = app.listen(80);

如果您输出以下内容:

console.log(typeof express);
console.log(typeof app);

您将看到以下内容:

function
function

它们都是功能.但是,JavaScript中的函数也是对象,并且可以具有属性.

They are both functions. But, functions in Javascript are objects too and can have properties.

因此,您可以执行以下操作:

So, you can do things like this:

function myFunction() {
    return "ok";
}

myFunction.greeting = "Hello";

console.log(myFunction());                 // "ok"
console.log(myFunction.greeting);         // "Hello"

如果要这样做,请进一步查看expressapp:

Looking further at both express and app, if you did this:

console.log(Object.getOwnPropertyNames(express));

您会得到的:

[
  'length',         'name',
  'prototype',      'application',
  'request',        'response',
  'Route',          'Router',
  'json',           'query',
  'raw',            'static',
  'text',           'urlencoded',
  'bodyParser',     'compress',
  'cookieSession',  'session',
  'logger',         'cookieParser',
  'favicon',        'responseTime',
  'errorHandler',   'timeout',
  'methodOverride', 'vhost',
  'csrf',           'directory',
  'limit',          'multipart',
  'staticCache'
]

而且,这个:

console.log(Object.getOwnPropertyNames(app));

会得到这个:

[
  'length',          'name',            'prototype',
  'constructor',     '_events',         '_eventsCount',
  '_maxListeners',   'setMaxListeners', 'getMaxListeners',
  'emit',            'addListener',     'on',
  'prependListener', 'once',            'prependOnceListener',
  'removeListener',  'off',             'removeAllListeners',
  'listeners',       'rawListeners',    'listenerCount',
  'eventNames',      'init',            'defaultConfiguration',
  'lazyrouter',      'handle',          'use',
  'route',           'engine',          'param',
  'set',             'path',            'enabled',
  'disabled',        'enable',          'disable',
  'acl',             'bind',            'checkout',
  'connect',         'copy',            'delete',
  'get',             'head',            'link',
  'lock',            'm-search',        'merge',
  'mkactivity',      'mkcalendar',      'mkcol',
  'move',            'notify',          'options',
  'patch',           'post',            'propfind',
  'proppatch',       'purge',           'put',
  'rebind',          'report',          'search',
  'source',          'subscribe',       'trace',
  'unbind',          'unlink',          'unlock',
  'unsubscribe',     'all',             'del',
  'render',          'listen',          'request',
  'response',        'cache',           'engines',
  'settings',        'locals',          'mountpath',
  'router'
]

因此,您可以看到它们除了具有功能外,还具有很多特性.

So, you can see they each have a lot of properties in addition to being functions.

好的,请告诉我我是否正确. 1)当我这样做时... const express = require(express)我将一个类"存储到express变量中. 2)然后,当我这样做时... express.json()我正在访问Express类中的json()函数吗?

Ok please tell me if I have got this correct. 1) When I do this… const express = require("express") I store a "Class" into the express variable. 2) Then when I do this… express.json() I am accessing the json() function inside the express class ?

正如我在上面的回答中所说,express变量代表工厂函数.该函数在被调用时会为您创建一个对象.这是直接调用new myObj()中的构造函数的另一种创建对象的方式. express.json是一个函数,该函数在调用时会为您创建一个中间件函数,该中间件函数使用传递给该函数的参数.

As I said in my answer above that express variable represents a factory function. That's a function that, when called, creates an object for you. It's a different way of creating an object from directly calling a constructor as in new myObj(). express.json is a function that, when called, creates a middleware function for you that uses the parameters you passed the function.

Express体系结构与纯类样式体系结构有所不同.它使用一个工厂函数来创建一个实例(本质上是一个类).并且,app代表该实例,但它本身也可以用作请求处理程序.

The Express architecture differs a bit from a pure class style architecture. It uses a factory function that creates an instance (essentially of a class). And, then the app represents that instance, but also works as a request handler on its own.

这篇关于当我需要“表达"时我需要知道它是对象还是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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