class Person {
name = 'Max' // property, 클래스에 정의한 변수
call = () => {...} // method, 클래스에 정의한 함수
// new 키워드로 클래스의 인스턴스 생성 활용
const myPerson = new Person()
myPerson.call()
console.log(myPerson.name)
클래스를 갖고 객체를 생성할 수 있다.
클래스에서는 상속을 사용할 수 있다.
class Human {
constructor() {
this.gender = 'male';
}
class Person extends Human {
constructor() { <- 생성자함수
super(); // 상위클래스의 생성자함수를 실행.
this.name = 'Max';
this.gender = 'femail';
} // this 키워드로 Person의 프로퍼티 설정.
printMyName() {
console.log(this.name);
}
}
const person = new Person();
person.printMyName();
person.printGender();
spread & rest operators
spread operators
oldArray 를 추가하고싶으면
const newArray = [...oldArray, 1, 2]
const newObject = {...oldObject, newProp:5}
rest operators : 함수의 인수 목록을 배열로 합치는데 사용
function sortArgs(...args) {
return args.sort()
} 무한대의 매개변수 사용 가능~. 매개변수가 배열로 통합 됨.
const filter = (...args) => {
return args.filter(el => el === 1);
}
console.log(filter(1,2,3))
출력결과
[1] // filter로 배열을 필터링 한 것임.
destructuring(구조분해할당)
프로퍼티를 하나만 가져와서 변수에 저장
Array Destructuring
[a,b] = ['Hello', 'Max']
console.log(a) // Hello
console.log(b) // Max
const numbers = [1, 2, 3];
[num1, , num3] = numbers;
console.log(num1, num3); // 1,3
Object Destructuring
{name}={name:'Max', age:28}
console.log(name) // Max
console.log(age) // undefined
참조형, 원시형 데이터 타입
number, string, boolean 기본형 자료타입
재할당하거나 변수를 다른변수에 저장할때마다 값을 실제로 복사.
객체와 배열은 참조형 자료타입임.
const person = {
name : 'Max'
}
const secondPerson = person; // 객체 재할당
console.log(secondPerson); // Max
person.name = 'Manu';
console.log(secondPerson); // Manu
객체 person({name: 'Max'})은 메모리에 저장되어 있고,
상수(const person) person에는 메모리에 있는 주소를 가리키는 포인터를 저장.
즉, secondPerson에 person을 할당하면 포인터가 복사되는 것임.
const secondPerson = {...person}; // 객체로부터 프로퍼티와 프로퍼티의 값을 가져와서 새로 생성한 객체에 추가. (진짜 복사본 생성)
console.log(secondPerson); // Max
배열 함수
filter, sort, map 등등.
const numbers = [1,2,3];
const doubleNumArray = nuimbers.map(() => {
return
});
map 메소드는 모든 값을 새로운 배열로 저장해줌.
'개발 저장소 > 개발 지식 저장소' 카테고리의 다른 글
[Javascript] new Date(2022, 11, 09) 구문 사용 시 error!! (0) | 2022.12.09 |
---|---|
[Javascript] 함수스코프와 블록스코프의 차이 (0) | 2022.12.09 |
[Javascript] 생성자함수와 프로토타입 (0) | 2022.12.08 |
[Javascript] Import & Export (0) | 2022.12.08 |
[Javascript] 일반 함수와 화살표 함수(arrow function)의 차이 (0) | 2022.12.08 |