简体   繁体   English

在 JavaScript 中将函数声明为常量

[英]Declaration of functions in JavaScript as constants

Why do we use "const" declarator in declaration of functions in Javascript?为什么我们在 Javascript 中的函数声明中使用“const”声明符?


     const functionName= () => {
           ...
        }

const functionName = () => {
       return 15;
    }

Here an anonymous(lambda) function's result is assigning to a constant variable 'functionName'.这里匿名(lambda)函数的结果是分配给一个常量变量“functionName”。 Just like const x = 5;就像 const x = 5;

Lambda functions are used when you need a function for a short period of time.当您在短时间内需要某个函数时,可以使用 Lambda 函数。 It has no function name.它没有函数名。 This is commonly used when you want to pass a function as an argument to higher-order functions, that is, functions that take other functions as their arguments.这通常用于将函数作为参数传递给高阶函数,即以其他函数作为参数的函数。

// Function declaration
function add(){
}
// A variable declared
const add=()=>{
}

Function promotion occurs in function declarations,Variable declarations are not promoted函数提升发生在函数声明中,不提升变量声明

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

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