TodayILearned/javascript

The Complete JavaScript Course 2021(2)

tuigun 2021. 6. 23. 23:30

2021.06.22 - [TodayILearned/javascript] - The Complete JavaScript Course 2021(1)

 

The Complete JavaScript Course 2021(1)

유데미의 the complete javascript course를 듣고 정리한 내용입니다. 자바스크립트란? 자바스크립트는 고수준 언어이고, 객체 기반의 언어Object-oriented programming (OOP)이며, 멀티 패러다임 언어이다. 자바..

tuigun.tistory.com

이어서 쓰는 글

 

Function calling other Fucntion, 함수내에서 또다른 함수 호출하기

함수 내에서 또다른 함수를 호출하여 사용이 가능하다.
DRY (Don't repeat your self)원칙에 의해 아래와 같이 함수를 나눈다.
만약 나누지 않는다면, apples * 4 , oranges * 4를 각각 써줘야하는데,
동일한 내용의 코드가 반복되는것은 잠재적인 버그의 원인이 된다.
function cutFruitPieces(fruit) {
  return fruit * 4
}
  
function fruitProcessor(apples, oranges) {
	// call function inside another function
	const applePieces = cutFruitPieces(apples);
	const orangePieces = cutFruitPieces(oranges);
	const juice = `Juice with ${applePieces} apples and ${orangePieces} oranges.`;
	return juice;
}
console.log(fruitProcessor(2, 3));
// DRY principle (don't repeat your self

함수 복습

1. 함수 선언식 (function declaration)
  함수가 선언되기 전까지 사용할 수 없음
2. 함수 표현식 (fuction express)
  함수 value가 변수에 저장됨
3. 화살표 함수 (arrow function)
  한 줄 짜리 코드를 쓰는데 좋고, 빠르다.
  this 키워드를 갖지 않는다. 
4. 함수의 구성요소
  함수명
  파라미터 : 입력값, 함수 내의 범위를 갖는다.
  실행코드
  인자 : 파라미터에 들어가는 실질적인 값
  변수 : 함수의 값을 저장하는 역할

return -1

일반적으로 -1은 프로그래밍 언어에서 아무것도 아님을 뜻함. 

배열

배열을 생성함과 동시에 값을 저장할 수 있음
const friends = ["Micheal", "steven", "peter"]
push()는 배열 뒤쪽에 요소를 추가하고, 추가된 길이만큼의 배열을 return하는 함수이다.
const friends = ["Micheal", "steven", "peter"];
// push function은 새로운 array를 return한다.
// ADD elements
const newLength = friends.push("Jay");
console.log(friends); //[ 'Micheal', 'steven', 'peter', 'Jay' ]
console.log(newLength); // 4
unshift()는 배열 요소를 앞에 추가하고, 추가된 길이 만큼의 배열을 return하는 함수이다.
const friends = ["Micheal", "steven", "peter"];
// push function은 새로운 array를 return한다.
// ADD elements
const newLength = friends.push("Jay");
console.log(friends);
console.log(newLength);

friends.unshift("John");
console.log(friends); // [ 'John', 'Micheal', 'steven', 'peter', 'Jay' ]
pop()은 배열의 마지막 요소를 제거합니다.
const friends = ["Micheal", "steven", "peter"];
// push function은 새로운 array를 return한다.
// ADD elements
const newLength = friends.push("Jay");
console.log(friends);
console.log(newLength);

friends.unshift("John");
console.log(friends);

// Remove elements
const popped = friends.pop(); // Last element
console.log(popped); //Jay
console.log(friends); //[ 'John', 'Micheal', 'steven', 'peter' ]
shift()는 배열 앞쪽의 요소를 제거합니다.
const friends = ["Micheal", "steven", "peter"];
// push function은 새로운 array를 return한다.
// ADD elements
const newLength = friends.push("Jay");
console.log(friends);
console.log(newLength);

friends.unshift("John");
console.log(friends);

// Remove elements
const popped = friends.pop(); // Last element
console.log(popped);
console.log(friends);

friends.shift(); // First
console.log(friends); //[ 'Micheal', 'steven', 'peter' ]
indexOf()는 배열 요소의 인덱스를 반환합니다.
console.log(friends.indexOf("steven")); // 1
console.log(friends.indexOf("Bob")); // 존재하지 않는 요소는 -1을 반환합니다.
ES6에서 새로 추가된, includes는 배열 내 요소의 여부에 대해 boolean값을 반환합니다.