简体   繁体   English

JavaScript 箭头 Function 问题,语法问题

[英]JavaScript Arrow Function question, syntax problem

First off, sorry for the vague title, I honestly don't know how to explain it without directly showing.首先,对不起这个模糊的标题,我真的不知道如何解释它没有直接显示。 I am new to JavaScript and have been learning arrow functions, however, I do not recognize this type of arrow function/syntax.我是 JavaScript 的新手,并且一直在学习箭头函数,但是,我不认识这种类型的箭头函数/语法。

Currently, I am creating a website and am adding a session cookie using a passport with mongoose.目前,我正在创建一个网站,并使用带有 mongoose 的护照添加一个 session cookie。 The tutorial I am using has a very peculiar way of using the arrow function and am unable to find any information online pertaining to the syntax.我正在使用的教程有一种使用箭头 function 的非常特殊的方式,并且无法在线找到任何与语法有关的信息。

This is the code simplified这是简化的代码

foo(bar)(()=>{
    ... Code Here ...
});

I understand the arrow function, but what I do not understand is how the parenthesis work in this situation and what really is going on?我理解箭头 function,但我不明白括号在这种情况下是如何工作的,到底发生了什么?

        |
        V
foo(bar)(...)

I haven't been able to find anything online about this, and weird enough it runs without any errors when I run the function in the tutorial, however, if I do this with something like this,我无法在网上找到任何关于此的内容,而且很奇怪,当我在本教程中运行 function 时,它运行时没有任何错误,但是,如果我用这样的方法执行此操作,

function foo(){
    console.log("In foo");
}

foo()(()=>{
    console.log("In bar");
});

I will get an error saying我会收到一条错误消息

foo is not a function

The actual code being run will be down below if required如果需要,正在运行的实际代码将在下面

(This code is being run on a server, the req is the request data sent to a GET request, I am using passport JavaScript and mongoose here. The res variable is the response to the user, and yes this code works just fine no errors and it does what it needs to, I just don't understand how this syntax works). (此代码在服务器上运行,req 是发送到 GET 请求的请求数据,我在这里使用护照 JavaScript 和 mongoose。 res 变量是对用户的响应,是的,这段代码工作得很好,没有错误它做了它需要做的事情,我只是不明白这个语法是如何工作的)。

req.authenticate("local")
 ((req, res)=>{
   res.redirect("/");
 });

What you have there is a function foo being called with a parameter bar , which returns a function that is taking an arrow function as a parameter.您所拥有的是使用参数bar调用的 function foo ,它返回一个 function ,它将箭头 function 作为参数。
So the open bracket after the bar is where you are calling the function returned by foo .因此,栏后的左括号是您调用foo返回的 function 的位置。

 function foo(bar){ console.log(bar); return function(arrow){ arrow(); } } foo('bar')( ()=>{ console.log("In bar"); } );

req.authenticate("local") is returning a function that accepts another function, you can think of it like this: req.authenticate("local")返回一个接受另一个 function 的 function,你可以这样想:

// foo can be viewed as a function because the return value from
// req.authenticate("local") is a function
const foo = req.authenticate("local");

Now, if you think of foo as a regular function, then foo(bar) should be a familiar syntax to you现在,如果您将foo视为常规 function,那么foo(bar)应该是您熟悉的语法

The syntax works is because of the fact that req.authenticate("local") is returning a function, so that you can call it with subsequent arguments.语法有效是因为req.authenticate("local")返回 function,因此您可以使用后续的 arguments 调用它。

However, in your custom code但是,在您的自定义代码中

function foo() {
    console.log("In foo");
}

you are not returning a function, but implicitly returning undefined .您没有返回 function,而是隐式返回undefined As a result, the subsequent call will result in an error.因此,后续调用将导致错误。

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

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