본문 바로가기

JavaScript7

Part9 <배열 함수 10가지> 1. 배열을 string 문자열 하나로 합치기 => join const fruits = ['apple','banana','orange']; const result = fruits.join(' , '); =>괄호 안 구분자는 생략 가능 console.log(result); 2. string을 배열로 만들기 => split const fruits = 'apple,banana,cherry' const result = fruits.split(구분자,[제한자]); const result = fruits.split(',',2); -> ['apple','banana'] 3. 배열 내 데이터를 반대로 바꾸기 => reverse const array = [1,2,3,4,5]; const result = array.rev.. 2021. 12. 27.
Part 8 <Array 개념과 APIs> 비슷한 타입의 object들을 모아놓은 것(바구니) => 자료구조 1. 배열 선언 2가지 방법 const arr1 = new Array(); const arr2 = [1,2] =>0번에 1, 1번에 2 2. Index position const fruits = ['apple','banana']; console.log(fruits); console.log(fruits.length); ->2 console.log(fruits[0]); -> apple 배열의 마지막 데이터에 접근할 때 console.log(fruits[fruits.length - 1]); 3. looping over an array console.clear(); 1) for(let i = 0; i fruits.forEach((fruit) =>.. 2021. 12. 26.
Part 7 <Object> 1. literals & property primitive type은 변수 당 하나의 값만 담을 수 있음 const name = 'ellie'; const age = 4; print(name, age); function print(name, age) { console.log(name); console.log(age); } 문제는 담아야 할 정보 많으면 코드 점점 길어짐 하지만 object로 하면 function print(person) { console.log(person.name); console.log(person.age); } const ellie = {name: 'ellie', age:4}; print(ellie); *Object 만드는 방법 1. const obj1 = { };//object l.. 2021. 12. 26.
Part 6 <Class vs Object> *class -template, declare once, no data in *object -instance of a class, created many times, data in JavaScript 에서 class는 ES6에서 추가됨 기존의 프로토타입에 기반하여 간편하게 문법만 추가됨 1. 클래스 선언 class Person { //constructor constructor(name, age) { //fields this.name = name; this.age = age; } //methods speak() { console.log(`${this.name}: hello!`); } } *object만들기 const ellie = new Person('ellie', 20); console.log(ellie.. 2021. 12. 26.