기본 문법
배열
객체
문자열 처리
함수
자료구조
알고리즘
비동기 처리
기타
Set
과 Map
등의 내장 객체 사용reduce
, filter
, find
, findIndex
최적화 및 디버깅
먼저 객체는 뭘까. 객체는 자바스크립트의 데이터 타입 중 하나다. 객체는 키-값의 쌍으로 데이터를 저장한다. 이런 키-값 쌍을 우리는 속성(property)라고 부른다.
객체는 데이터와 함수(메서드)를 함게 그룹화하는 것에 유용하다.
let person = {
name: 'John', // 속성
age: 30, // 속성
greet: function() { // 메서드
console.log('Hello, ' + this.name);
}
};
person.greet(); // "Hello, John" 출력
let keys = Object.keys(person);
console.log(keys); // ["name", "age", "greet"]
let values = Object.values(person);
console.log(values); // ["John", 30, ƒ]
let entries = Object.entries(person);
console.log(entries); // [["name", "John"], ["age", 30], ["greet", ƒ]]
먼저 프로토 타입이 뭘까. 프로토 타입은 객체지향 프로그래밍에서 중요한 개념 중 하나이다. 그러나 우리가 원래 알던 다른 언어의 클래스 기반 상속과는 조금 다르게 동작한다.
자바스크립트에서 모든 객체는 내부적으로 다른 객체를 참조하는 숨겨진 속성인 [[Prototype]] 를 가진다. 대부분의 경우 이 속성은 해당 객체의 생성자의 prototype 속성을 참조한다.
이렇게 연결된 객체 체인을 프로토타입 체인이라고 부른다.
자바스크립트는 프로토타입 기반 언어이다. 객체는 다른 객체의 프로토타입으로 사용될 수 있다.
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.greet = function() {
console.log('Hello, ' + this.name);
};
let john = new Person('John', 30);
john.greet(); // "Hello, John" 출력
여기서 john객체는 Person의 인스턴스이다.
우리는 Person의 프로토타입에 정의된 greet 메서드를 사용할 수 있다.
let str = "Hello, world!";
console.log(str.substring(0, 5)); // "Hello"
console.log(str.substr(0, 5)); // "Hello"
console.log(str.slice(0, 5)); // "Hello"
let str = "Hello, world!";
let position = str.indexOf("world");
console.log(position); // 7 출력
console.log("apple,banana,orange".split(",")); // ["apple", "banana", "orange"] 출력
console.log(str.replace("world", "JavaScript")); // "Hello, JavaScript!" 출력
console.log(" Hello ".trim()); // "Hello" 출력
console.log(str.includes("world")); // true 출력