Playing with data structures in Javascript - Lee-hyuna/33-js-concepts-kr GitHub Wiki

μžλ°”μŠ€ν¬λ¦½νŠΈλŠ” 맀일 μ§„ν™”ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. React , Angular , Vue , NodeJS , Electron , React Native 와 같은 ν”„λ ˆμž„μ›Œν¬ 및 ν”Œλž«νΌμ΄ λΉ λ₯΄κ²Œ μ„±μž₯함에 따라 큰 규λͺ¨μ˜ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ— μžλ°”μŠ€ν¬λ¦½νŠΈλ₯Ό μ‚¬μš©ν•˜λŠ” 것이 일반적이 λ˜μ—ˆμŠ΅λ‹ˆλ‹€. μžλ°”μŠ€ν¬λ¦½νŠΈκ°€ jquery에 μ˜μ‘΄ν•˜λ˜ μ‹œλŒ€λŠ” μ§€λ‚¬μŠ΅λ‹ˆλ‹€. λ˜ν•œ μ΄μ œλŠ” νŠΉμ • λ¬Έμ œμ— μ ν•©ν•œ 툴이 무엇인지 μ΄ν•΄ν•˜κ³ , 효율적이고 μ„±λŠ₯이 λ›°μ–΄λ‚œ μ‘μš©ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λŠ” 것이 μ€‘μš”ν•©λ‹ˆλ‹€.

μ΄λŸ¬ν•œ μ΄μœ μ—μ„œ 데이터 ꡬ쑰 λŠ” 맀우 μ€‘μš”ν•©λ‹ˆλ‹€. μžκΈ°μ£Όλ„ ν•™μŠ΅ ν”„λ‘œκ·Έλž˜λ¨Έμ˜€κΈ° λ•Œλ¬Έμ— μ‘Έμ—… ν• λ•ŒκΉŒμ§€ 데이터 ꡬ쑰λ₯Ό 곡뢀 ν•œ 적이 μ—†μŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ μ΅œκ·Όμ— λ‚˜λŠ” μžλ°”μŠ€ν¬λ¦½νŠΈλ₯Ό 기반으둜 ν•œ λŒ€κ·œλͺ¨ μ‘μš© ν”„λ‘œκ·Έλž¨μ„ μž‘μ—…ν•˜κ³  있으며, μ˜¬λ°”λ₯Έ μƒν™©μ—μ„œ μ˜¬λ°”λ₯Έ 도ꡬλ₯Ό μ‚¬μš©ν•˜λŠ” 것이 μ€‘μš”ν•˜λ‹€λŠ” 것을 κΉ¨λ‹¬μ•˜μŠ΅λ‹ˆλ‹€. ( μ—¬κΈ°μ„œ 도ꡬ λŠ” 데이터 κ΅¬μ‘°λ§Œμ„ μ§€μΉ­ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μΆ”κ°€ λΈ”λ‘œκ·Έ κ²Œμ‹œλ¬Όμ—μ„œ λ‹€λ£° λ‚΄μš©). μ½”λ“œ μƒμ—μ„œ μ•½κ°„μ˜ κ°„λ‹¨ν•œ μˆ˜μ • λ§ŒμœΌλ‘œλ„ μ‘μš© ν”„λ‘œκ·Έλž¨μ˜ μ„±λŠ₯이 크게 ν–₯상 될 수 μžˆμŠ΅λ‹ˆλ‹€.

이 첫 번째 λΈ”λ‘œκ·Έ κΈ€μ—μ„œλŠ” κΈ°λ³Έ 데이터 ꡬ쑰 인 μŠ€νƒμœΌλ‘œ μ‹œμž‘ ν•©λ‹ˆλ‹€.

μŠ€νƒ

μŠ€νƒμ€ μ΄ν•΄ν•˜κΈ° μ‰½μŠ΅λ‹ˆλ‹€. μœ„μ˜ 이미지λ₯Ό 보면 μŠ€νƒμ˜ λŒ€λΆ€λΆ„μ˜ 속성을 이해할 수 μžˆμŠ΅λ‹ˆλ‹€. μœ„μ— ν‘œμ‹œλœ μŠ€νƒμ˜ 첫 번째 ν•­λͺ©μ€ 'λΉ¨κ°„' λΆ€λΆ„μž…λ‹ˆλ‹€.( 'out'이 쓰지연). κ·ΈλŸ¬λ‚˜ 이 ν•­λͺ©μ„ μ œκ±°ν•˜λ €λ©΄ λ‹€λ₯Έ ν•­λͺ©λ“€μ„ λ¨Όμ € μ œκ±°ν•΄μ•Όλ§Œ ν•˜κΈ° λ•Œλ¬Έμ— κ°€μž₯ λ§ˆμ§€λ§‰μ— 제거될 κ²ƒμž…λ‹ˆλ‹€. κ°„λ‹¨νžˆ λ§ν•΄μ„œ μŠ€νƒμ˜ 첫 번째 ν•­λͺ©μ€ λ§ˆμ§€λ§‰μ— 제거 될 ν•­λͺ©μž…λ‹ˆλ‹€.

μ½”λ“œλ₯Ό 눈으둜 λ³Έλ‹€λ©΄ 더 λͺ…ν™• ν•΄μ§‘λ‹ˆλ‹€. ES6 ꡬ문을 μ‚¬μš©ν•˜μ—¬ 더 κΉ”λ”ν•˜κ³  μ΄ν•΄ν•˜κΈ° μ‰½κ²Œ λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€.

μŠ€νƒμ˜ λͺ¨μŠ΅μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

class Stack {  
	constructor(){  
	  
	}  
	  
	push(item){  
	//push item to the stack  
	}  
	  
	pop(){  
	//pull out the topmost item (last item) from stack  
	}  
	  
	peek(){  
	// see what's the last item in stack  
	}  
	  
	size(){  
	//no. of items in stack  
	}  
	  
	isEmpty(){  
	// return whether the stack is empty or not  
	}  
}

ν•¨κ»˜ μ½”λ“œλ₯Ό μž‘μ„±ν•΄ λ΄…μ‹œλ‹€. λ‚΄λΆ€μ μœΌλ‘œ 배열을 μ‚¬μš©ν•˜μ—¬ μŠ€νƒμ˜ ν•­λͺ©μ„ λ‹€μŒκ³Ό 같이 μ €μž₯ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

class Stack {  
  constructor(){  
    this._items = [];  
  }  
  ...

λ³€μˆ˜ 이름 μ•žμ˜ _ (밑쀄)은 이 λ³€μˆ˜κ°€ λ‚΄λΆ€μ μœΌλ‘œ 만 μ‚¬μš©λ˜μ–΄μ•Όν•˜λ©° μŠ€νƒμ˜ μ–΄λ–€ μΈμŠ€ν„΄μŠ€λ„ 이 λ³€μˆ˜μ— μ ‘κ·Όν•΄μ„  μ•ˆλœλ‹€λŠ” 것을 μ „λ‹¬ν•˜κΈ° μœ„ν•΄ μ‚¬μš© λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

μŠ€νƒμ˜ λ‹€λ₯Έ λˆ„λ½λœ 뢀뢄을 κ΅¬ν˜„ν•΄ λ΄…μ‹œλ‹€.

class Stack {  
	constructor(){  
		this._items = []  
	}  
	  
	push(item){  
		//push item to the stack  
		return this._items.push(item)  
	}  
	  
	pop(){  
		//pull out the topmost item (last item) from stack  
		return this._items.pop()  
	}  
	  
	peek(){  
		// see what's the last item in stack  
		return this._items[this._items.length-1]  
	}  
	  
	size(){  
		//no. of items in stack  
		return this._items.length  
	}  
	  
	isEmpty(){  
		// return whether the stack is empty or not  
		return this._items.length==0  
	}  
}

μŠ€νƒμ˜ κΈ°λ³Έ ꡬ쑰λ₯Ό μ™„μ„±ν–ˆμŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ μ—¬μ „νžˆ κ΅¬ν˜„ν•˜κ³  싢은 λͺ‡ 가지 κΈ°λŠ₯이 μžˆμŠ΅λ‹ˆλ‹€.

  1. μ΄ˆκΈ°ν™”ν•˜λ©΄μ„œ μŠ€νƒμ— 일뢀 ν•­λͺ©μ„ 전달 ν•  수 μžˆμ–΄μ•Όν•©λ‹ˆλ‹€.
  2. μ—¬λŸ¬ ν•­λͺ©μ„ ν•œκΊΌλ²ˆμ— μΆ”κ°€ν•˜κ³ , μ œκ±°ν•  수 μžˆμ–΄μ•Όν•©λ‹ˆλ‹€.
class Stack {  
	constructor(...items){  
	this._items = []  
	  
	if(items.length>0)  
		items.forEach(item => this._items.push(item))
	}  
	  
	push(...items){  
		//push item to the stack  
		items.forEach(item => this._items.push(item) )  
		return this._items;
	}  
	  
	pop(count=0){  
		//pull out the topmost item (last item) from stack  
		if(count===0)  
			return this._items.pop()  
		else  
			return this._items.splice( -count, count )  
	}  
	  
	peek(){  
		// see what's the last item in stack  
		return this._items[this._items.length-1]  
	}  
	  
	size(){  
		//no. of items in stack  
		return this._items.length  
	}  
	  
	isEmpty(){  
		// return whether the stack is empty or not  
		return this._items.length==0  
	}  
}

훨씬 λ‚˜μ•„ λ³΄μž…λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ μ—¬μ „νžˆ λͺ‡ 가지 λ¬Έμ œμ μ„ κ³ μ³μ•Όν•©λ‹ˆλ‹€.

  1. μ™ΈλΆ€μ—μ„œ λˆ„κ΅¬λ‚˜ _items λ³€μˆ˜μ— μ•‘μ„ΈμŠ€ν•˜μ—¬ λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€. ES6μ—μ„œ WeakMap 을 μ‚¬μš©ν•˜μ—¬ 이 문제λ₯Ό ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  2. μ „μ—­ λ„€μž„ 슀페이슀λ₯Ό μ˜€μ—Όμ‹œν‚€μ§€ μ•ŠμœΌλ €λ©΄ Stack의 λ„€μž„μŠ€νŽ˜μ΄μŠ€λ₯Ό μ˜¬λ°”λ₯΄κ²Œ μ§€μ •ν•΄μ•Όν•©λ‹ˆλ‹€. 이λ₯Ό μœ„ν•΄ 전체 κ΅¬ν˜„μ„ ν΄λ‘œμ €λ‘œ κ°μŒ‰λ‹ˆλ‹€.
  3. μŠ€νƒμ—μ„œ ν˜„μž¬ ν•­λͺ©μ„ λ³Ό μˆ˜μžˆλŠ” 방법이 ν•„μš”ν•©λ‹ˆλ‹€. 이λ₯Ό μœ„ν•΄ toArray() λ©”μ†Œλ“œλ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€.

μ•„λž˜λŠ” μ΄λŸ¬ν•œ λ¬Έμ œκ°€ ν•΄κ²° 된 μ΅œμ’… κ΅¬ν˜„ μž…λ‹ˆλ‹€.

let Stack = (()=>{  
 let map = new WeakMap();  
 let _items = [];
 
 class Stack {  
  constructor(...items){  
    // let's store our items array inside the weakmap  
    map.set(this, []);  
    // let's get the items array from map, and store it in _items for easy access elsewhere  
    _items = map.get(this);  
      
    //if constructor receives any items, it should be stacked up  
    this.push(..items)  
        
  }  
    
  push(...items){  
    //push item to the stack  
     items.forEach(item => _items.push(item) )  
     return _items;  
      
  }  
    
  pop(count=1){  
    //pull out the topmost item (last item) from stack  
       return _items.splice( -count, count )  
  }  
    
  peek(){  
    // see what's the last item in stack  
    return _items[_items.length-1]  
  }  
    
  size(){  
    //no. of items in stack  
    return _items.length  
  }  
    
  isEmpty(){  
    // return whether the stack is empty or not  
    return _items.length==0  
  }  
    
   toArray(){  
     return _items;  
   }}
   
   return Stack;
 })();

μ΅œμ’… κ΅¬ν˜„μ€ λ˜ν•œ μ•„λž˜μ—μ„œ 확인 κ°€λŠ₯ν•©λ‹ˆλ‹€. https://gist.github.com/anish000kumar/0fd37acc866a9577cf259980500b1bbe

μŠ€νƒ μ‚¬μš©

λ‹€μŒμ€ μš°λ¦¬κ°€ λ§Œλ“  μŠ€νƒμ„ μ‚¬μš©ν•˜λŠ” μž‘μ€ 예제 μž…λ‹ˆλ‹€.

let my_stack = new Stack(1,24,4);  
// [1, 24, 4]

my_stack.push(23);
//[1, 24, 4, 23]

my_stack.push(1,2,342);  
//[1, 24, 4, 23, 1, 2, 342]

my_stack.pop();  
//[1, 24, 4, 23, 1, 2]

my_stack.pop(3);
//[1, 24, 4]

my_stack.isEmpty();
// false

my_stack.size();  
// 3

my_stack.toArray();  
//[1, 24, 4]