函数声明有两大优势:
目的明了,举个例子吧:
看看这行代码:
const maxNumberOfItemsInCart = ...;
读完了还是不知道这省略符号是函数还是其他什么值。它可能是:
const maxNumberOfItemsInCart = 100;
也可能是:
const maxNumberOfItemsInCart = (statusPoints) => statusPoints * 10;
如果使用函数声明就没有这个麻烦了。
请看:
const maxNumberOfItemsInCart = 100;
对比:
function maxNumberOfItemsInCart(statusPoints) {
return statusPoints * 10;
}
2. Order of declaration == order of execution
执行顺序和声明顺序一致时,是最理想的。如果能做到执行时才声明使用 const(常数)关键字的值就很厉害了。