简体   繁体   English

如何确定nodejs/expressjs函数中可用的方法

[英]How to determine the methods avaialble in nodejs/expressjs functions

If I have a function like如果我有一个像

request.get(options, function(req, res, body) {
    console.log(req, res, body)
});

Are the local functions variable names arbitrary (req,res,body).局部函数变量名称是任意的(req、res、body)。 Like can they be anything?他们可以是任何东西吗? If that's the case, how does 'request' know how to interpret each function variable?如果是这样,“请求”如何知道如何解释每个函数变量? Is it by the placement order.. as in the first req/whatever is always going to be a request?是按照放置顺序.. 就像第一个请求/任何总是会成为请求的东西一样? How do I know if there should be more than 3 local function variables and what they will do?我怎么知道是否应该有 3 个以上的局部函数变量以及它们会做什么?

Are the local functions variable names arbitrary (req,res,body).局部函数变量名称是任意的(req、res、body)。 Like can they be anything?他们可以是任何东西吗?

Yes, anything you like.是的,任何你喜欢的。 (That said: Using consistent names makes it easy for people to understand your code. Using the standard names from the Express documentation makes it even easier.) (也就是说:使用一致的名称可以让人们更容易地理解你的代码。使用 Express 文档中的标准名称使它更容易。)

If that's the case, how does 'request' know how to interpret each function variable?如果是这样,“请求”如何知道如何解释每个函数变量?

It doesn't.它没有。 It calls the function you call it with arguments, like this:它使用参数调用您调用它的函数,如下所示:

// Hypothetical example of within the part of `request` that triggers the callback:
theCallback(theRequest, theResponse, theBody);

...and those arguments are bound to your parameters ( req , res , body ) by position : The first argument request passes your callback is bound to req , the second to res , and the third to body . ...并且这些参数通过位置绑定到您的参数( reqresbody ):第一个参数request传递您的回调绑定到req ,第二个到res ,第三个到body

"Ah," you may be wondering, "but what if I don't define a body parameter and there are only two?" “啊,”你可能想知道,“但如果我不定义一个body参数并且只有两个呢?” Doesn't matter;没关系; in JavaScript, it's fine if we call a function with more arguments than it has parameters (or fewer).在 JavaScript 中,如果我们调用一个参数多于它的参数(或更少)的函数,那很好。

This is just like any other function call.这就像任何其他函数调用一样。 The caller doesn't know the parameter name, just what arguments it passes and the order in which it should pass them.调用者不知道参数名称,只知道它传递的参数以及传递它们的顺序。

For instance (see comments):例如(见评论):

 // A fake "request" function function myRequest(callback) { // (Using a timer to make our callback happen later, just to more closely match `request`) setTimeout(function() { // Note that this doesn't know or care what the parameter // names are you've used in your callback callback("one", "two", "three"); }, 250); } // Your use of it myRequest(function(a, b, c) { // a, b, and c are bound by position, not name console.log(a, b, c); });

It does binding by order .按顺序绑定

This means that this code:这意味着这段代码:

request.get(options, function(req, res, next)...

req is a request because it appears first. req是一个请求,因为它首先出现。 The name you choose ( req , res ) have no impact on how the data is bound.您选择的名称( reqres )对数据的绑定方式没有影响。

How do I know if there should be more than 3 local function variables and what they will do?我怎么知道是否应该有 3 个以上的局部函数变量以及它们会做什么?

You either read the ExpressJS implementation code (you shouldn't) or you can read their docs or examples here .您可以阅读 ExpressJS 实现代码(您不应该阅读),或者您可以在此处阅读他们的文档示例

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

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