● 오늘 공부한 것
- 반복문 9문제, 조건문 13문제를 페어와 같이 리뷰!
● 내가 놓친 것
function foo(a,b,c){
console.log(a) // 1
console.log(b) // 2
console.log(c) // undefinded
}
foo(1,2)
- 위와 같이 함수에 인자에 매개변수보다 적게 값을 적으면 주지 않은 값은 undefinded로 출력된다.
function foo(a,b,c){
console.log(a) // 1
console.log(b) // 2
console.log(c) // 3
}
foo(1,2,3,4)
- 위와 같이 함수에 인자에 매개변수보다 더 많이 적으면 무시된다.
하지만 사실은 함수 매개변수들은 arguments, 즉 (array-like object) 유사 배열의 객체의 형태로 저장된다.
function foo(a, b, c) {
console.log(arguments.length); // 4
console.log(arguments[0]); // 1
console.log(arguments[1]); // 2
console.log(arguments[2]); // 3
console.log(arguments) // { 0 : 1, 1: 2, 2: 3, 3: 4}
}
foo(1,2,3,4)
arguments 객체는 유사 배열 객체이기 때문에 배열 메소드를 직접 사용할 수 없다. ex(.map, .filter, .reduce)
그러나, Array.from() 메소드나 전개 연산자(spread operator)를 사용하여 arguments 객체를 배열로 변환할 수 있다.
예를 들어, 다음과 같이 arguments 객체를 배열로 변환할 수 있다.
function foo(a,b,c) {
const args = Array.from(arguments);
console.log(args); // [1, 2, 3]
}
foo(1,2,3)
function foo(a,b,c) {
const arg = [...arguments];
console.log(arg) // [1, 2, 3]
}
foo(1,2,3)
- new Regex
- 문자열과 두 개의 문자(from, to)를 입력받아, 문자열에 등장하는 특정 문자(from)가 다른 문자(to)로 바뀐 문자열을 리턴해야 합니다.
let str = "dddeeeaaafff";
// from이라는 문자들을 모두 f로 바꾸고 싶다.
let from = 'de';
let to = 'f';
str = str.replace(/from/g, to);
위와 같이 str.replace 메서드에 정규식사이에 변수를 넣었더니 적용되지 않았다. 즉, 변수가 아니라 정규식을 따로 선언해주어야한다.
let str = "dddeeeaaafff";
// from이라는 문자들을 모두 f로 바꾸고 싶다.
let from = 'de';
let to = 'f';
let reg = new RexExp(from, 'g');
str = str.replace(reg, to);
● 끝맺음
- 📍 수업이외 학습
- CS지식 2개 포스팅
- 모던 자바스크립트 1시간동안 정독하기
- redux 공부 (따로 포스팅예정) / 30프로 들었다 니꼬쌤
- 프로그래머스 4페이지 진입!!
- 인터렉티브..웹페이지 만들기