简体   繁体   English

我不知道有什么区别

[英]I don't know the difference

I've just started learning about function.我刚刚开始学习函数。 and here is what gives me difficulty.这就是给我带来困难的原因。

 const Func = () => { return () => { console.log('hello'); }; }; const innerFunc = Func(); innerFunc(); // 1. Func(); //2.

I don't understand why 'hello' doesn't show up on console trying with 2.我不明白为什么“你好”没有出现在尝试使用 2 的控制台上。

Aren't both innerFunc and Func names of function?函数的名称不是innerFunc 和Func 吗?

I don't know the difference between them.我不知道它们之间的区别。

Sorry for my bad English.对不起,我的英语不好。

Func() returns a function which will not be executed until you run the result with () like you are doing with 1. . Func()返回一个函数,在您使用()运行结果之前不会执行该函数,就像您使用1.所做的那样。
By doing const innerFunc = Func();通过做const innerFunc = Func(); , you are assigning the function returned from Func into innerFunc . ,您正在将从Func返回的函数分配给innerFunc So to call it, you need to run it like this: innerFunc() .所以要调用它,你需要像这样运行它: innerFunc()

Your function 'Func' returns another function from inside, so when you call Func(), then it will return another function which you named as 'innerFunc' and calling innerFunc will return the output as hello.你的函数'Func'从内部返回另一个函数,所以当你调用Func()时,它会返回另一个你命名为'innerFunc'的函数,调用innerFunc将返回输出为hello。

When you try with Just 'Func()', it will just return another function which you returned inside Func.当您尝试使用 Just 'Func()' 时,它只会返回您在 Func 中返回的另一个函数。

Starting to learn about functions, just look at开始学习函数,看看

const Func = () => {
    console.log('hello');
};

Func(); // logs hello to the console

What you have here is a function inside of function, which is more complicated and not necessarily beginner friendly.你这里有一个函数内部的函数,它更复杂,不一定适合初学者。

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

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