javascript中使用到的变量,尽量在当前作用域的头部集体声明,变量声明应在一条声明语句中完成:只使用一个命令(var/let/const),各个变量名用逗号隔开。执行一条语句的速度高于执行多条语句的速度。
注:引用变量,以及常量尽量使用 const ,而普通变量则尽可能使用 let
反例
function func(){var count = 5;var color = 'blue';var value = [1, 2, 3];var now = new Date();var students = {name: '张三',num: '13356477'};//do something...}{let count = 5;let color = 'blue';let value = [1, 2, 3];let now = new Date();let students = {name: '张三',num: '13356477'};}const count = 5;const color = 'blue';const value = [1, 2, 3];const now = new Date();const students = {name: '张三',num: '13356477'};
正例
function func(){var count = 5,color = 'blue',value = [1, 2, 3],now = new Date(),students = {name: '张三',num: '13356477'};//do something...}{let count = 5,color = 'blue',value = [1, 2, 3],now = new Date(),students = {name: '张三',num: '13356477'};}const count = 5,color = 'blue',value = [1, 2, 3],now = new Date(),students = {name: '张三',num: '13356477'};
