JavaScript/입 속의 검은 잎
-
클래스 호이스팅JavaScript/입 속의 검은 잎 2022. 10. 18. 10:52
먼저, 클래스는 함수의 일종이다. 클래서 선언문으로 정의한 클래스는 소스코드 평가과정, 즉 런타임 이전에 먼저 평가되어 함수 객체를 생성한다. 이때 클래스가 평가되어 생성된 함수 객체는 생성자 함수로서 호출할 수 있는 함수, 즉 constructor다. 생성자 함수로서 호출할 수 있는 함수는 함수 정의가 평가되어 함수 객체를 생성하는 시점에 프로토타입도 더불어 생성된다. 프로토타입과 생성자 함수는 단독으로 존재할 수 없고, 언제나 쌍으로 존재하기 때문이다. , 421쪽. const Jedi = ''; { //호이스팅이 발생하지 않는다면 ''이 출력되어야 한다. console.log(typeof Jedi) //ReferenceError: Cannot access 'Jedi' before initializa..
-
클래스, super, 오버라이딩JavaScript/입 속의 검은 잎 2022. 10. 12. 10:53
const Jedi = function(name, age){ this.name = name; this.age = age; this.isJedi = true; this.force = ()=>{console.log('may the force be with you')}; }; const Anakin = new Jedi('Anakin', 30); console.log(Anakin) class Sith { constructor(name, age){ this.name = name; this.age = age; this.isJedi = false; } force(){ console.log('may the force serve you') }; } const DarthVader = new Sith('DarthVader'..
-
async/awaitJavaScript/입 속의 검은 잎 2022. 10. 5. 20:27
처음 나를 애먹였던 것은 아마도 await 였다. 몇가지 시시한 요인이 있다. 1.어휘 뜻이 그냥 좀 딜레이를 두겠다는 것 같다, 2.그런데 사용 양상은 마치 딜레이를 없애고 다시 동기화시키는 것 같다. 좀 더 직관적으로 비동기처리를 할 수 있도록, 비동기적 과정을 표면상에서는 생략하도록 만들어진 것이, 오히려 비동기적 사고를 따라가려는 나에게 또다른 에러가 되었던 것 같다. 하여튼 지금은 이렇게 이해한다. 비동기처리를 위한 콜백함수도, 프로미스도, 그리고 저 await도, "오이디푸스"이다. 다른 뜻이 아니라 자기 실현적 예언이라는 틀에서 그렇다. 자기실현적인 콜백, 약속(promise), 잠깐만(await)의 예언적 순간들 말이다. 이것은 서스펜스와 개연성의 원천이며 인류의 보편이기도 하다. 그래서 ..
-
promise catch errorJavaScript/입 속의 검은 잎 2022. 10. 5. 18:32
function 몇초후프로미스에러발생(sec){ return new Promise((resolve,reject)=>{ setTimeout(()=>{ reject('error!'); }, sec*1000); }) } try{ 몇초후프로미스에러발생(3); } catch(err){ console.error(err) } """ node:internal/process/promises:279 triggerUncaughtException(err, true /* fromPromise */); ^ [UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by ..
-
try catch errorJavaScript/입 속의 검은 잎 2022. 10. 5. 17:57
function f2(){ console.log('f2 start'); throw '에러' console.log('f2 end') } function f1(){ console.log('f1 start') try{ f2(); }catch(err){ console.log(err); } console.log('f1 end') } console.log('will : f1') f1(); console.log('did : f1') /* will : f1 f1 start f2 start 에러 f1 end did : f1 */ 로그를 보면, 에러 발생 이후 f2() 의 두번째 console.log 는 실행되지 않았다. 이후 catch 문에 잡히고 나서 f1 end 는 콘솔에 찍혔다. function f2(){ cons..
-
json 객체JavaScript/입 속의 검은 잎 2022. 10. 5. 11:15
JavaScript Object Notation, JSON 흔히 보는 객체의 배열 같지만 추가 규칙이 있다. - 값을 표혈할 때 문자열, 숫자, 불 자료형만 가능. 함수 불가. - 문자열은 반드시 큰 따옴표. -키에도 반드시 큰 따옴표. 자바스크립트 객체를 JSON 문자열로 변환할 때 JSON.stringify() 사용. const data =[ {name : '가나다라', 속성 : 'red'}, {name : '마바사아', 속성 : 'blue'} ] console.log(JSON.stringify(data)) // [{"name":"가나다라","속성":"red"},{"name":"마바사아","속성":"blue"}] console.log(JSON.stringify(data, "속성", 2)) /** [ ..
-
set()JavaScript/입 속의 검은 잎 2022. 9. 28. 13:42
//기존배열 let arr = []; arr.push(1); arr.push(5); arr.push(5); console.log(arr) // [ 1, 5, 5 ] let set1 = new Set() set1.add(1) set1.add(5) set1.add(5) console.log(set1) // Set(2) { 1, 5 } // foreach 로 돌리면서 검사 arr = ['Seoul','Busan','Jeju'] let isExistEoul = false; arr.forEach(item => { if (item=='Seoul') isExistSeoul = true; }) //set의 내장함수로 검사 let arr2 = new Set(arr) console.log(arr2.has('Seoul'))..
-
문자열.charCodeAt(index)JavaScript/입 속의 검은 잎 2022. 9. 28. 00:30
35번과 36번 문자열 비교의 경우. function solution(strings, n) { return strings.sort().sort((a,b) => a.charCodeAt(n) - b.charCodeAt(n)) } -charCodeAt( index) 안의 index가 문자열의 그 index인 것이 헷갈렸었다. -제한조건, "n번째 문자열이 같을 때는 본래의 사전순서대로" 라는 조건을 구현하려 시도하다가... 대단히 헤맸다. 어디까지 갔다왔나 몰라. let s = "Zbcdefg" function solution(s) { let n = [...s] // ['Z', 'b', 'c','d', 'e', 'f', 'g'] return n.sort((a,b) => b.charCodeAt(0)-a.char..