给出我的代码
const express = require("express")
const app = express()
console.log(typeof app)
console.log(typeof express)
我的终端告诉我这两个变量都是函数!
这对我来说毫无意义,因为我可以访问它们中的每个属性,就好像它们是对象一样,但是当我在 console.log 中执行 typeof 时,我的终端告诉我它们都是函数。
有人可以向我解释一下这些变量是什么以及为什么可以像访问对象一样访问它们的属性吗?
最佳答案
when I require “express” I need to know if its an object or function
两者都是。 Javascript 中的函数是对象。
无需深入了解,express
是一个创建应用程序实例的工厂函数,而 app
是一个 express 实例。现在,从技术上讲,express
和 app
都是函数。但是,Javascript 中的函数是对象并且可以具有属性。 express 对象具有静态属性。 app
对象的行为很像 express 类的一个实例。它既有方法又有实例数据。
现在,获得更多技术......
const express = require('express')
为您提供一个函数,该函数也是一个对象并具有属性。
这个特定函数是一个工厂函数,当您调用它时它会创建一个 app
对象,它也是一个具有属性的函数。
因此,express
也有可以使用的属性,例如:
express.static
express.json
express.urlencoded
而且,它可以这样调用:
const app = express();
同样,app
也是一个具有属性的函数。它可以用作函数,如下所示:
const server = http.createServer(app);
server.listen(80);
或者,它可以像对象一样使用:
const server = app.listen(80);
如果你输出这个:
console.log(typeof express);
console.log(typeof app);
你会看到这个:
function
function
它们都是函数。但是,Javascript 中的函数也是对象并且可以具有属性。
所以,你可以这样做:
function myFunction() {
return "ok";
}
myFunction.greeting = "Hello";
console.log(myFunction()); // "ok"
console.log(myFunction.greeting); // "Hello"
进一步查看 express
和 app
,如果您这样做:
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'
]
所以,你可以看到它们除了是函数之外,还有很多属性。
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 thejson()
function inside the express class ?
正如我在上面的回答中所说,express
变量代表一个工厂函数。这是一个函数,当被调用时,它会为您创建一个对象。这是一种不同于直接调用构造函数创建对象的方法,如 new myObj()
。 express.json
是一个函数,当被调用时,它会为您创建一个中间件函数,该函数使用您传递给该函数的参数。
Express 架构与纯类风格的架构略有不同。它使用一个工厂函数来创建一个实例(本质上是一个类)。然后,app
表示该实例,但它自己也可以作为请求处理程序。
关于javascript - 当我需要 "express"时,我需要知道它是对象还是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59764123/