// Don't do this kind of thing yet://------ lib.js ------exportfunctionsquare(x){returnx*x;}exportfunctiondiag(x,y){returnsqrt(square(x)+square(y));}//------ main.js ------import{square,diag}from'lib';
여러 문자열이 관련되어있는 경우 특히 복잡한 문자열 연결에 대해 템플릿 문자열 (`로 구분)을 사용하십시오.
// badfunctionsayHi(name){return'How are you, '+name+'?';}// badfunctionsayHi(name){return['How are you, ',name,'?'].join();}// badfunctionsayHi(name){return`How are you, ${ name }?`;}// goodfunctionsayHi(name){return`How are you, ${name}?`;}
8. 긴 문자열에는 줄 연속을 사용하지 마십시오.
ES5에서는 이것을 허용하지만 슬래시 뒤에 공백이 오는 경우에는 까다로운 오류가 발생할 수 있습니다.
// bad (sorry, this doesn't show up well on mobile)constlongString='This is a very long string that \
far exceeds the 80 column limit. It unfortunately \
contains long stretches of spaces due to how the \
continued lines are indented.';// goodconstlongString='This is a very long string that '+'far exceeds the 80 column limit. It does not contain '+'long stretches of spaces since the concatenated '+'strings are cleaner.';
eval 또는 Function (... 문자열) 생성자를 사용하지 마십시오 (코드 로더 제외).
이러한 기능은 잠재적으로 위험하며 단순히 CSP 환경에서 작동하지 않습니다.
// badletobj={a:20,b:30};letpropName=getPropName();// returns "a" or "b"eval('var result = obj.'+propName);// goodletobj={a:20,b:30};letpropName=getPropName();// returns "a" or "b"letresult=obj[propName];// obj[ "a" ] is the same as obj.a
11. 상수는 밑줄로 구분 된 ALL_UPPERCASE (대문자+언더스코어로만 구성) 로만 명명한다.
// badconstnumber=5;// goodconstNUMBER=5;
12. 큰 따옴표가 아닌 작은 따옴표 사용
팁 : 문자열에 작은 따옴표 문자가 포함되어 있으면 따옴표를 이스케이프하지 않아도되도록 템플릿 문자열을 사용하는 것이 좋습니다.
// badletdirective="No identification of self or mission."// badletsaying='Say it ain\u0027t so.';// goodletdirective='No identification of self or mission.';// goodletsaying=`Say it ain't so`;
마지막으로 ...
이것들은 명령이 아닙니다. Google은 많은 기술 대기업 중 하나 일뿐입니다.
개인적으로 Airbnb의 사양이 Google보다 더 매력적이라고 생각합니다.
이러한 특정 규칙을 취하는건 모든 종류의 코드를 작성할 때 문체의 일관성을 염두에 두는 것이 중요합니다.