搜狐首页 科技 宫主大人

手机搜狐

SOHU.COM

简洁的javascript编码(一)--变量、函数

一、变量

constyyyymmdstr=moment().format('YYYY/MM/DD'); Good

constcurrentDate=moment().format('YYYY/MM/DD');

// 86400000是什么鬼? setTimeout(blastOff, 86400000) ; Good

// 通过大写常量来定义.constMILLISECONDS_IN_A_DAY = 86400000;setTimeout(blastOff, MILLISECONDS_IN_A_DAY);

constaddress = ' OneInfinite Loop, Cupertino 95014'; constcityZipCodeRegex = /^[^,]+[,s]+(.+?)s*( d{5})?$/;saveCityZipCode(address. match(cityZipCodeRegex)[1], address. match(cityZipCodeRegex)[2]); Good

constaddress = ' OneInfinite Loop, Cupertino 95014'; constcityZipCodeRegex = /^[^,]+[,s]+(.+?)s*( d{5})?$/; const[, city, zipCode] = address. match(cityZipCodeRegex) || ;saveCityZipCode(city, zipCode);

constlocations = [ 'Austin', 'New York', 'San Francisco'];locations. forEach((l) = { doStuff(); doSomeOtherStuff(); // ... // ... // ... // 等等 我忘记了l是啥了?dispatch(l);}); Good

精选