一流大学和一流专业
In programming, a first-class function is when a function is treated as a variable. This is special because it means that a function can be passed into another function as an argument.
在编程中, 一等函数是指将函数视为变量的情况。 这很特殊,因为它意味着一个函数可以作为参数传递给另一个函数。
Not all languages support a first-class function idea, and these are usually natively imperative languages like C. Java and C++ also didn’t use to support first-class based ideas.
并非所有语言都支持一流的功能概念,而这些通常是本机命令性语言,例如C。Java和C ++也不用于支持基于一流的概念。
Imperative programming tells a computer how to do something — it’s a set of linear instructions that are executed in order. Declarative programming follows a generalized what should happen sequence. The control flow in a declarative language is not as strict and code can be executed asynchronously when required.
命令式编程告诉计算机如何做某事-这是一组按顺序执行的线性指令。 声明式编程遵循应该发生的一般顺序。 声明性语言的控制流程不那么严格,可以在需要时异步执行代码。
JavaScript is declarative by design and nature. The order of execution isn’t as vital towards arriving at the desired end-state for declarative programming.
JavaScript在设计和性质上都是声明性的。 执行顺序对于达到声明式编程所需的最终状态并不那么重要。
Why is this important?
为什么这很重要?
When it comes to first-class functions, you can assign a function to a variable but the actual value of the variable will not be set until it gets called. It’s a fuzzy area because the assignment does not happen instantly. Imperative programming requires clarity and first-class functions introduces a level of uncertainty.
对于一流的函数,您可以将函数分配给变量,但是直到变量被调用后才设置其实际值。 这是一个模糊的领域,因为分配不会立即发生。 命令式编程要求清晰,一流的函数引入了一定程度的不确定性。
Take a look at the following JavaScript code:
看一下下面JavaScript代码:
let catName = function(){
return "Tibbers";
}
In JavaScript, functions are not invoked until they are called. The above code is valid but catName
is not set until it is called. The invocation will execute the function attached to it and assign catName
with the value Tibbers
.
在JavaScript中,只有在调用函数后才调用它们。 上面的代码有效,但是直到调用catName
才设置。 调用将执行附加到该函数的函数,并为catName
分配值Tibbers
。
When you assign a function directly to a variable, it’s called an anonymous function and you can call the variable by adding a pair of parentheses at the end. So to invoke (and therefore assign) the above variable, you’ll need to write catName()
当您直接将函数分配给变量时,该函数称为匿名函数 ,您可以通过在末尾添加一对括号来调用该变量。 因此,要调用(并因此分配)上述变量,您需要编写catName()
To use a function as an argument, you need to pass in the function name.
要将函数用作参数,您需要传入函数名称。
For example:
例如:
let catName = function(){
return "Tibbers";
}
function greet(name){
console.log("hello " + name);
}
greet(catName());
catName
is passed in as an argument for the greet()
function. If you use catName
without the parenthesis ()
, JavaScript will pass the contents of the function rather than execute it. This allows the function to be treated as a value.
catName
作为greet()
函数的参数传入。 如果您使用不带括号()
catName
,则JavaScript将传递函数的内容而不是执行它。 这样可以将该函数视为一个值。
The flow of execution looks something like this:
执行流程如下所示:
![Image for post](https://miro.medium.com/max/9999/1*kJmsjjKTa5Jc0Gmok595ng.png)
With first-class functions, you can also have a function return another function. This is called a higher-order function.
使用一流的函数,您还可以让一个函数返回另一个函数。 这称为高阶函数 。
In JavaScript code, it can look something like this:
在JavaScript代码中,它看起来可能像这样:
function calculateCart() {
//do something here
return cartTotal();
}
This is valid JavaScript code because a function returned is treated as a value. You can also write the same thing using a variable.
这是有效JavaScript代码,因为返回的函数被视为值。 您也可以使用变量编写相同的内容。
const calculateCart = function(){
//do something here
return cartTotal();
}
Why is the above set to const
and not let
or var
? const
ensures that the structure of the function cannot be changed later down the track, making it immutable by default. In JavaScript, immutability refers to the inability to change the values assigned. In the above example, a function is assigned as an immutable thing, which means that the process cannot be changed but the values being processed and passing through it can.
为什么将以上设置为const
而不是let
或var
? const
确保函数的结构在以后无法更改,从而使其在默认情况下不可变。 在JavaScript中, 不变性是指无法更改分配的值。 在上面的示例中,一个函数被分配为一个不变的事物 ,这意味着该过程无法更改,但正在处理和传递的值可以更改。
The return
can also be in the form of an anonymous function. For example:
return
值也可以采用匿名函数的形式。 例如:
const calculateCart = function(){
//do something here
return function(){
//do something here
}
}
let myCart = calculateCart();
myCart();
When you have higher-order functions, you need to invoke each level manually. If you don’t do this, it will return the function itself rather than the final expected return.
当您具有高阶函数时,需要手动调用每个级别。 如果不这样做,它将返回函数本身,而不是最终的预期收益。
Why is this? Because functions are not executed unless they are called. This call has to be explicit.
为什么是这样? 因为除非调用函数,否则函数不会执行。 该调用必须是明确的。
That’s why for calculateCart
to work properly, you need to assign it to another value first before invoking the chain.
因此, calculateCart
正常工作,您需要在调用链之前首先将其分配给另一个值。
Here’s what the execution chain looks like in a diagram:
这是执行链在图中的样子:
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/ef42379e60c3513e5a5e70e497bb15ef.png)
Alternatively, you can also execute the nested function through double parenthesis.
另外,您也可以通过双括号执行嵌套函数。
const calculateCart = function(){
//do something here
return function(){
//do something here
}
}
calculateCart()();
Or if you’ve got more than two nested levels, you just need to add the same amount of parenthesis to ensure that the inner functions get invoked.
或者,如果您具有两个以上的嵌套级别,则只需添加相同数量的括号以确保调用内部函数。
const calculateCart = function(){
//do something here
return function(){
//do something here
return function(){
//do something here
}
}
}
calculateCart()()();
That’s basically first-class functions in a nutshell for JavaScript.
简而言之,这基本上是一流的函数。
翻译自: https://medium.com/madhash/what-exactly-are-first-class-functions-811b3268c759
一流大学和一流专业