js 7

[JS][ES6] 객체

class를 통한 객체 생성 function Health(name){ this.name = name; } Health.prototype.showHealth = function(){ console.log(this.name + "님 안녕하세요"); } const h = new Health("crong"); h.showHealth(); >> "crong님 안녕하세요" javascript에는 class가 없지만 다음과 같이 prototype을 이용해 비슷한 구조를 만들 수 있다. class Health { constructor(name, lastTime){ this.name = name; this.lastTime = lastTime; } showHealth(){ console.log("안녕하세요 "+ this...

JSTS 2022.06.05

[JS][ES6] set, map

Set으로 유니크한 배열 만들기 : 중복없이 유일한 값을 저장하려고 할때, 이미 존재하는지 체크할 때 유용 let mySet = new Set(); // 자료형 console.log(toString.call(mySet)); >> "[object Set]" // set에 인자 추가 mySet.add("crong"); mySet.add("hary"); mySet.add("crong"); // 해당 요소가 있는지 불린값 반환 console.log(mySet.has("crong")); >> true // 요소 삭제 mySet.delete("crong"); // 요소 하나씩 출력 mySet.forEach(function(v){ console.log(v); }) >> "hary" WeakSet : 참조를 가지고 있..

JSTS 2022.06.05

[JS][ES6] const, let

Babel을 사용하여 ES6 문법으로 코드를 작성 후 ES5로 변환해주어 호환성을 보장하는 방식이 주로 쓰인다. let : let은 블록단위에서 사용 가능한 변수 선언이다. var name = "global var"; function home(){ var homevar = "homevar"; for(let i=0; i ["apple", "orange", "watermelon", "banana"] immutable array는 어떻게 만들까? const list = ["apple","orange","watermelon"]; list2 = [].concat(list, "banana"); console.log(list, list2); console.log(list === list2); >> ["apple", ..

JSTS 2022.06.05