반응형

Javascript 12

[JavaScript] try catch문

에러가 발생하는 경우 출력(7번 행) try { console.log('시작'); const test = '테스트'; console.log(test); test = 'fasd'; const lang = 'JavaScript'; console.log(lang); } catch(err){ console.log('에러'); console.error(err); console.log(err.name); console.log(err.message); } 에러가 발생하지 않을 때 출력 try { console.log('시작'); const test = '테스트'; console.log(test); // test = 'fasd'; const lang = 'JavaScript'; console.log(lang); } c..

[JavaScript] 에러 / 에러객체

에러객체의 종류 1. Reference Error : 존재하지 않는 변수나 함수 참조 2. Type Error : 변수를 함수처럼 사용할 때 - 잘못된 자료형 3. Syntax Error : 문법에 맞지 않는 코드 4. 기타 https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Error Error - JavaScript | MDN Error 객체는 런타임 오류가 발생했을 때 던져집니다. Error 객체를 사용자 지정 예외의 기반 객체로 사용할 수도 있습니다. 아래 표준 내장 오류 유형을 참고하세요. developer.mozilla.org 에러를 만들기 const error = new TypeError('타입에러 발생'..

[JavaScript] 상수 변수

const로 선언된 상수는 변경 불가능 let 으로 선언된 변수는 변경 가능 - 코드 길어지면 변수가 어떤 값을 가질 지 모르기 때문 const로 선언된 객체의 프로퍼티의 값은 변경 가능 let x = 1; x = 4; const y = x; y = 3; x = 2; console.log(x); → const변수를 변경하려 해서 error 발생 let team1 = ['Drum', 'Bass', 'Saxophone']; const team2 = team1; team1.splice(2, 1, 'Trumpet'); team2.splice(2, 1, 'Piano'); console.log(team1); console.log(team2); // [ 'Drum', 'Bass', 'Piano' ] // [ 'Dru..

벡엔드/NodeJS 2023.12.24

[JavaScript] 참조형

배열은 slice() 사용가능 let numbers1 = [1, 2, 3]; let numbers2 = numbers1.slice(); numbers2.push(4); console.log(numbers1); // [ 1, 2, 3 ] console.log(numbers2); // [ 1, 2, 3, 4 ] 참조형은 복사가 불가능함(= 활용) 1) assign 메소드를 이용한 복사 let course1 = { title : 'title', language: 'JavaScript' }; let course2 = Object.assign({}, course1); course2.title = 'type'; console.log(course1); title 값 그대로 console.log(course2); ti..

카테고리 없음 2023.12.23

[JavaScript] 문자열

문자 찾기 let myString = 'Hello world'; console.log(myString[3]); // l 출력 console.log(myString.charAt(3)); // l 출력 인덱스 console.log(myString.indexOf('o')); // 4 console.log(myString.lastIndexOf('o')); // 7 대문자, 소문자 console.log(myString.toUpperCase()); console.log(myString.toLowerCase()); console.log(myString.trim()); // 양쪽 공백 지우기 slice console.log(myString.slice(0,2)); // He 0번 ~ 1번 인덱스까지 console.log..

카테고리 없음 2023.12.22

[JavaScript] then 메소드

console.log('START!'); fetch('https://jsonplaceholder.typicode.com/users') .then((response) => response.text()) .then((result) => {console.log(result); }) console.log('END'); then 메소드 - Promise 객체가 fulfilled 상태가 되었을 때, 실행할 callback을 등록하는 메소드 - Promise 객체가 fulfilled 상태가 되면, 작업 성공 결과(서버가 보내 준 response)를 갖는 데 response이다. → fetch 함수는 Promise 객체 return -> fulfilled 상태가 되면 -> then 메소드 등록

카테고리 없음 2023.05.25

[NodeJS] fetch 함수

fetch 함수 실행 console.log('START!'); fetch('https://jsonplaceholder.typicode.com/users') .then((response) => response.text()) .then((result) => {console.log(result); }) console.log('END'); 결과 START! END [ { "id": 1, ... → response 내용이 END 보다 늦게 출력 됨 처음 START 출력 request 보냄. then 메소드는 callback 함수를 등록할 뿐, 실행하지 않음. 그리고 다음 End 출력 서버로부터 response가 도착하고, then 메소드로 등록된 callback들이 실행됨(비동기 실행). 동기 실행: 한 번 시작..

카테고리 없음 2023.05.22

[Web API] Method 메소드

POST request 수행 const newMember = { name: 'Jerry', email: 'Jerry@codeitmall.kr', department: 'engineering', }; fetch("https://learn.codeit.kr/api/members", { method: 'POST', body: JSON.stringify(newMember), }) .then((response) => response.text()) .then((result) => {console.log(result)}); - POST Request 응답 - POST Request 수행 후 network 확인 --> GET 메소드에서는 Response 확인 불가 ※ name, email, department 중 일부..

카테고리 없음 2023.05.22
반응형