简体   繁体   English

解释这个 function 是如何工作的

[英]Explain how this function works

 let a = () => { return ((b) => alert(b))("hello"); }; a();

This function alerts "hello", please explain to me how this works and what type of a function this is?这个 function 提醒“你好”,请向我解释这是如何工作的以及这是什么类型的 function?

It's a function which, inside, calls an immediately-invoked function expression (IIFE) - ie a function that is declared and called in the same line.这是一个 function ,它在内部调用一个立即调用的 function 表达式(IIFE) - 即在同一行中声明并调用的 function 。

The return does nothing (it returns the return value from the alert() , which is undefined; nothing is listening for the return.) return 什么都不做(它从alert()返回未定义的返回值;没有任何东西在监听返回。)

Both the outer function and the IIFE use what's called arrow functions - short-syntax functions that don't have their own this context.外部 function 和 IIFE 都使用所谓的箭头函数 - 没有自己的this上下文的短语法函数。

The whole thing could be reduced to simply:整个事情可以简化为:

let a = () => alert('hello');
a();

(b) => alert(b) is an arrow function (b) => alert(b)是一个箭头 function

((b) => alert(b)) our arrow function is actually an anonymous function expression , and we need to wrap it between parentheses in order to invoke it in place ((b) => alert(b))我们的箭头 function 实际上是一个匿名的 function 表达式,我们需要将它包裹在括号之间以便就地调用它

((b) => alert(b))("hello") we pass "hello" as an argument to our function when invoking it ((b) => alert(b))("hello")我们在调用它时将"hello"作为参数传递给我们的 function

() => { return... } is yet another anonymous arrow function , except this time, instead of invoking the function in place, we store in a variable named a so that we can invoke it later () => { return... }是另一个匿名箭头 function ,除了这次,我们没有就地调用 function ,而是存储在一个名为a的变量中,以便我们以后可以调用它

a() calls the previously stored function, which in turn calls (b) => alert(b) with the argument "hello" which in turn calls alert("hello") a()调用先前存储的 function,然后调用(b) => alert(b)并使用参数"hello" ,然后调用alert("hello")

Let's focus on the让我们专注于

((b) => alert(b))("hello");

first.第一的。 ((b) => alert(b)) is a function that take a parameter b, and return alert(b). ((b) => alert(b)) 是一个 function,它接受一个参数 b,并返回 alert(b)。 And the ("hello") here, is a parameter that is send to this function.这里的 ("hello") 是发送到这个 function 的参数。

Soo, about this whole expression.苏,关于整个表达。 I can present another form that'll work我可以提出另一种可行的表格

let a = () => {
    ((b) => alert(b)) ("hi")
}
a()

here you don't even have to have a return.在这里,您甚至不必退货。 Alert return nothing anyway.无论如何,警报什么都不返回。 So basically, in this block, you define a function a, that take no parameters, and execute the code that i already explained.所以基本上,在这个块中,你定义了一个不带参数的 function a,并执行我已经解释过的代码。

Arrow functions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions箭头函数: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

IIFE: https://developer.mozilla.org/en-US/docs/Glossary/IIFE IIFE: https://developer.mozilla.org/en-US/docs/Glossary/IIFE

Same as如同

let a = () => {
  return ((b) => alert(b))("hello");
};

a();

a is a named function let a = a是一个名为 function let a =

a is an arrow function () => {} a是箭头 function () => {}

a is a parameter-less arrow function since the parenthesis is empty () a是无参数箭头 function 因为括号为空()

((b) => alert(b))("hello"); is an immediately invoked function since it is inside the ()是立即调用的 function 因为它在()

(b) => alert(b) is an arrow function with parameter b that alerts the b value (b) => alert(b)是一个箭头 function 带有参数b来提醒b

((b) => alert(b))("hello"); gets the value "hello" since the ("hello") passes the immediately invoked function using that value as the parameter value for b获取值"hello" ,因为("hello")使用该值作为b的参数值传递立即调用的 function

So given all that, let's see some examples:因此,鉴于所有这些,让我们看一些示例:

 let a = () => { return ((b) => alert(b))("hello"); }; a(); // same net effect ((b) => alert(b))("hellos"); // same net effect alert("hellosa"); (function(x){alert(x)})("hello Me") // https://developer.mozilla.org/en-US/docs/Glossary/IIFE let af = function() { return ((b) => alert(b))("helloaf"); }; af(); let na = function(nb) { return ((b) => alert(b))(nb); }; na("hellob"); let nab = function(nbb) { return function(bi){alert(bi);}(nbb); }; nab("hellobb"); function nab2(nbb) { return function(bi){alert(bi);}(nbb); }; nab2("hello2"); /* two functions */ function sayHi(bi){ alert(bi); } function nab3(nbbn) { return sayHi(nbbn); }; nab3("hello3");

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

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