// bad
const f = function(){};
const g = function (){};
const h = function() {};
// good
const x = function () {};
const y = function a() {};
참고 : Airbnb Style Guide - Functions
var의 scope
var로 선언한 변수는 같은 scope 내에서 재선언, 재할당 가능함
// 재선언
var greeting = "hey hi";
var greeting = "say Hello instead";
// 재할당
var greeting = "hey hi";
greeting = "say Hello instead";
console.log("greeting");
var greeting = "say hello";
// 위 코드 작동 순서
var greeting;
console.log("greeting"); // undefined
var greeting = "say hello";
let greeting = "say hi";
let times = 4;
if (time > 3) {
let hello = "say hello instead";
console.log(hello); // "say hello instead"
}
console.log(hello); // Error : hello is not defined
// 재할당 가능
let greeting = "hey hi";
greeting = "say Hello instead";
// 재선언 불가능
let greeting = "hey hi";
let greeting = "say Hello instead"; // Error: Identifier 'greeting' has already been declared
let greeting = "say hi";
if (true) {
let greeting = "say hello instead";
console.log(hello); // "say hello instead"
}
console.log(hello); // "say hi"
const의 scope
const로 선언한 변수는 같은 scope 내에서 재할당, 재선언 모두 불가능함
// 재할당 불가능
const greeting = "hey hi";
greeting = "say Hello instead"; // Error: Assignment to constant variable
// 재선언 불가능
const greeting = "hey hi";
const greeting = "say Hello instead"; // Error: Identifier 'greeting' has already been declared
const greeting = {
message: "say hi",
time: 4
}
greeting = {
message: "hello",
number: "five"
} // Error: Assignment to constant variable
greeting.message = "say hello instead"; // 가능함
참고 : Var, Let and Const
// 내 코드
$startButton.classList.add("invisible");
$startButton.classList.remove("invisible");
// 수정한 코드
const IVISIBLE = "invisible";
$startButton.classList.add(IVISIBLE);
$startButton.classList.remove(IVISIBLE);
early return : if문 안에서 return함으로서 뒤의 코드로 진입하지 않아 else문을 사용할 필요 없음
if-else 문 대신 early return을 사용하기
// 내 코드
function nextCard() {
if (cardNumber < totalQuizNumber) {
const card = data[cardNumber];
showCard(card);
markCard(card);
cardNumber++;
} else {
$questionBoard.textContent = "";
$codeBoard.textContent = "";
$choiceBoard.textContent = "";
$resultBoard.textContent = "수고하셨습니다!";
$restartButton.classList.remove("invisible");
$nextButton.classList.add("invisible");
}
...
}
// 수정한 코드
function nextCard() {
if (cardNumber < totalQuizNumber) {
const card = data[cardNumber];
showCard(card);
markCard(card);
cardNumber++;
return;
}
$questionBoard.textContent = "";
$codeBoard.textContent = "";
$choiceBoard.textContent = "";
$resultBoard.textContent = "수고하셨습니다!";
$restartButton.classList.remove("invisible");
$nextButton.classList.add("invisible");
...
}
파일 끝에 빈 줄(/n : newline character)을 넣어야함
line : A sequence of zero or more non-newline characters plus a terminating newline character
C 컴파일러인 gcc는 POSIX를 근거로 동작했는데, 소스코드를 한 줄씩 읽으므로 파일 끝에 빈 줄이 없으면 문제가 발생함
참고 : No newline at a end of file
self closing 해줘야함
사용자의 접근성을 위해 alt 속성을 항상 붙이는 것을 권장함
// 내 코드
<button type="button" class="restart-button invisible"><img src="./images/restart.png"></button>
<button type="button" class="next-button invisible"><img src="./images/next.png"></button>
// 수정한 코드
<button type="button" class="restart-button invisible"><img src="./images/restart.png" alt="restartButton" /></button>
<button type="button" class="next-button invisible"><img src="./images/next.png" alt="nextButton" /></button>