简体   繁体   English

Javascript 中 function arguments 的隐式传递是如何工作的?

[英]How does that implicit passing of function arguments in Javascript work?

I am originally coming from Java programming language as background.我最初来自 Java 编程语言作为背景。 Java is strongly typed and quite explicit (I mean by that, you have to write things out, you can't just omit them). Java 是强类型且非常明确的(我的意思是,你必须把事情写出来,你不能只是省略它们)。 One thing, that will never get in my head is how that implicit parameter passing to javascript works...一件事,我永远不会想到的是传递给 javascript 的隐式参数是如何工作的......

as an example:举个例子:

const observer = {
    next: console.log,
    error: console.error,
    test: console.table,
}

observer.next('HI World!')

in next, I specify console.log which is a function, but I never say to accept a value, but in Javascript I apparently can just throw anything to functions as suffix and it takes that as an argument to its function.接下来,我指定console.log,它是一个function,但我从来没有说过要接受一个值,但是在Javascript中,我显然可以将任何东西作为后缀扔给函数,并将其作为其ZC1C425268E68A784F114的参数。 Thats also how pipelining or currying basically works, it takes the remaining return values as parameters..why is that so?这也是流水线或柯里化的基本工作原理,它将剩余的返回值作为参数......为什么会这样?

Secondly, eg in express, I have a function signature like so:其次,例如在快递中,我有一个 function 签名,如下所示:

app.get('/', function (req, res) {
  res.send('GET request to the homepage');
});

I actually get req and res out of my function callback is that right?我实际上从我的 function 回调中得到了 req 和 res ,对吗? those are not parameters that I pass to my callback function?这些不是我传递给我的回调 function 的参数吗?

Edit : Can someone explain to me again where the REQ and RES parameters are coming from?编辑:有人可以再次向我解释REQRES参数的来源吗? How does that construct work?这个结构是如何工作的? Because I am defining the callback function myself, but instead of passing req and res as parameters to my callback, it seems they get passed from somewhere back to inside my callback function???!因为我自己定义了回调 function,但不是将 req 和 res 作为参数传递给我的回调,似乎它们是从某个地方传递回我的回调 function 内部的???!

A function in JavaScript is an object just like any other. JavaScript 中的 function 是 object 就像任何其他的一样。 This means a function can be passed around, can be assigned to variables, and essentially can be used anywhere a value can be used.这意味着 function 可以传递,可以分配给变量,并且基本上可以在任何可以使用值的地方使用。 This is known as first-class functions .这被称为一等函数

For example, given a simple function like this one:例如,给定一个简单的 function 像这样:

function log(msg) {
  console.log(msg);
}

These two functions are conceptually equivalent:这两个函数在概念上是等价的:

const log1 = log;

// same as

function log1(msg) {
  log(msg);
}

As you can see, we can directly assign the function log to a variable like log1 , ,and then we can use log1 just like we use log :如您所见,我们可以直接将 function log分配给log1这样的变量,然后我们可以像使用log一样使用log1

log1('foobar');

Another usage of first-class functions is to be able to pass them as arguments to other functions.一流函数的另一个用途是能够将它们作为 arguments 传递给其他函数。 A function that takes a callback like in your Express example is known as a higher-order function .像您的 Express 示例中那样采用回调的 function 被称为高阶 function The arguments of the callback are provided by the caller of that callback.回调的 arguments 由该回调的调用者提供。 In your case Express provides the req and res arguments.在您的情况下,Express 提供了reqres arguments。

Using callbacks is a form of inversion of control .使用回调是控制反转的一种形式。 You are passing control of that part of the program to the caller.您正在将程序那部分的控制权传递给调用者。

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

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