Class
function Health(name){ this.name = name; } Health.prototype.showHealth = function(){ console.log(this.name + '님 안녕하세요.'); } const h = new Health('gold'); h.showHealth(); class Health{ constructor(name, lastTime){ this.name = name; this.lastTime = lastTime; } showHealth(){ console.log('안녕하세요.' + this.name); } } const h = new Health('Gold'); h.showHealth(); 겉보기에는 class 이지만 toString.call(Health); 해..
2023.01.26
Arrow Function
let newArr1 = [1,2,3,4,5].map(function(v, i, o){ return v * 2; }) console.log(newArr1); let newArr2 = [1,2,3,4,5].map((v, i, o) => v * 3 ); console.log(newArr2); this context const myObj = { runTimeout(){ setTimeout(function(){ this.printData(); }, 200) }, printData(){ console.log('hihi'); } } myObj.runTimeout(); // error 1. const myObj = { runTimeout(){ setTimeout(function(){ this.printData(); ..
2023.01.26
Template
const data = [ { name : 'coffee bean', order : true, items : ['americano', 'milk'] }, { } ] function fn(tags, name, items){ console.log(tags); if(typeof items === 'undefined'){ items = '주문 가능한 상품이 없습니다.'; } return (tags[0] + name + tags[1] + items + tags[2]); } const template = fn`welcome ${data[0].name} !! 주문가능항목 ${data[1].items}`; console.log(template); const data = [ { name : 'coffee bean', o..
2023.01.26
Set
set 객체는 중복되지 않는 유일한 값의 집합 add 추가 has 값 유무 확인 delete 삭제 let mySet = new Set(); mySet.add('crong'); mySet.add('gold'); mySet.add('kwak'); console.log(mySet.has('gold')); mySet.delete('gold'); console.log(mySet.has('gold')); mySet.forEach(function(v){ console.log(v); })
2023.01.24
Obj
function getObj(){ const name = 'crong'; const getName = function(){ return name; } const setName = function(newName){ name = newName; } const printName = function(){ console.log(name); } return { getName, setName, name} } let obj = getObj(); console.log(obj); Destructuring 분해하다. Destructuring Array let data = ['crong', 'honux', 'jk']; let [jisu,,jung] = data; console.log(jisu, jung); // 'crong'..
2023.01.24
Array
for in과 for of 차이점 1. for...in은 객체를 반복할때 사용 2. for...of는 배열을 반복할때 사용 spread operator 기존에는 concat을 이용하여 배열을 합치곤 하였다. let pre = ['a', 'b', 100]; let newData = pre.concat(['c','d']) console.log(newData); ES6에서는 spread operator를 통해서 편하게 합칠 수 있게 되었다. let pre = ['a', 'b', 100]; let newData = [...pre]; console.log(pre, newData); //['a', 'b', 100], ['a', 'b', 100] console.log(pre === newData); //false n..
2023.01.24
scope
var ( variable )는 함수스코프이거나 전역스코프이다. 1. {} 밖에서도 접근이 가능하다. 2. 선언 전 사용 가능 ( 호이스팅 ) 3. 같은 이름으로 중복으로 선언가능 위와 같은 특징으로 부작용을 발생하며, let이 도입된 이유라고 할 수있다. let 은 블록스코프이다. 1. 소속된 블록 내에서만 사용가능 2. 중복 선언 불가능 const ( constant : 변함없는 ) 상수의 값을 저장한다. 1. 재할당이 안되지 추가, 변경은 가능하다. immutable array의 경우 let array = ['apple', 'banana']; let array2 = [].concat(array, 'orange'); console.log(array, array2);
2023.01.24