본문 바로가기

JavaScript

TypeScript 2 1. call signatures TypeScirpt에서 함수를 작성할 때에 항상 parmeter type과 return type을 함께 정의해야 한다. call signature 즉 미리 함수에 대해 type을 작성하고 이후에 함수를 만들어 사용할 수 있다. // 함수의 return type을 적지 않아도 된다, const add1 = (a:number, b:number) => a + b // call signatures : 함수에 마우스 올렸을 때 함수에 대해 알려주는 거 // 함수의 타입을 정의할 수 있다. 이는 parmeter, return 의 type을 미리 정의함을 말한다. type Add = (a:number, b:number) => number const add2:Add = (a, b) =.. 더보기
TypeScript 1 0. TypeScript를 사용하면 좋은 점 JavaScript의 경우 실행 전까지 오류를 감지 하지 못하지만 TypeScript 는 compile time에 오류를 미리 확인할 수 잇다. 1. Type Alias 내마음대로 정의 가능 optional 한 변수인 경우 ?을 붙여줌 변수 : type 과 같이 작성하여 type을 명시할 수 있다. 물론 Type Checker가 infer(추론) 하게 가만 두어도 된다(type은 변수 생성과 함께 정의 된다.) 함수의 경우 parameter와 return의 type을 각각 지정할 수 있다. arrow function으로도 작성 가능하다. // TypeScript // Type Alias 타입 정의 type Plyaer = { name:string, age?:n.. 더보기
Array.includes() Array에 특정 요소를 포함하는지를 판별한다. python에서 x in Array 와 같은 기능... 당연히 JS에도 있었다. const array1 = [1, 2, 3]; console.log(array1.includes(2)); // expected output: true const pets = ['cat', 'dog', 'bat']; console.log(pets.includes('cat')); // expected output: true console.log(pets.includes('at')); // expected output: false 더보기
_ (underscore)의미 1. 언더바 또는 언더스코어 "_" 주로 함수에서 중요하지 않은 파라미터의 값을 나타낼 때 쓴다. (일종의 관습) // Arrow Function 1 const arrowFunc1 = () => { // ... } // Arrow Function 2 const arrowFunc2 = _ => { // ... } 더보기
Array.from() 1. Array.from()은 유사배열객체를 받아 배열을 만들어준다. Array.from(arrayLike[, mapFn[, thisArg]]) 2. 1~8 값을 갖는 배열 만들기 Array.from({ length: 8 }, (v, i) => i + 1) // [1, 2, 3, 4, 5, 6, 7, 8] 3. 시퀀스 생성기(range) const range = (start, stop, step) => Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step)); range(0, 4, 1); // [0, 1, 2, 3, 4] range(1, 10, 2); // [1, 3, 5, 7, 9] 더보기
isAlpha() 구현하기 1. isAlpha() 주어진 String이 알파벳인지 확인 하고 싶다. 해당하는 함수는 JS에는 없다. search() 함수와 정규식(regex)을 사용하여 구현할 수 있다. function isAlpha(word){ return word.search(/[A-Za-z\s]/) != -1 } 2. isNaN() 숫자인지 확인할때는 isNaN()을 사용하면 된다. Number.isNaM()으로 하면 오류방지에 좀 더 낫다. 더보기
Hash and Map 1. Map The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value. for . . . of loop returns an array of [key, value] for each iteration 2. 관련 함수들 python의 dictionary와 다른 점은 접근, 수정시에 항상 set, get 매서드를 사용하여야 한다는 점이다. const contacts = new Map() contacts.set('Jessie', {phone: "213-55.. 더보기
REST API - Representational State Transfer - 웹서버, 웹브라우저, 모바일앱 간의 통신규칙? - HTTP 통신을 사용한다. - Resource : element로 이루어진 collection의 집합 (서버 쪽), URL로 나타낸다. - collection은 복수로 사용된다. ex) topics http:// restapi.example.com/sports/soccer : sports는 collection soccer는 element(or document) - HTTP METHOD : POST, GET, PUT, DELETE - Resource 명은 동사보다는 명사를 사용한다. - Resource 명에 HTTP Method가 들어가서는 안된다. - HTTP 응답 상태코드 : 200(GET.. 더보기