No Library No Framework - ChoDragon9/posts GitHub Wiki

바닐라

λ°°μ—΄ μ•„μ΄ν…œ μ‚­μ œ

const arr = [0, 1, 2]
arr.splice(1, 2)
console.log(arr) // [0]

μ²œλ‹¨μœ„ 콀마 ν‘œμ‹œ

const price = 10000
price.toLocaleString() // 10,000

μ •λ ¬

const arr = [0, 2, 3, 1]
arr.sort((a, b) => a - b)
console.log('μ˜€λ¦„μ°¨μˆœ', ...arr)
arr.sort((a, b) => b - a)
console.log('λ‚΄λ¦„μ°¨μˆœ', ...arr)

SVG Parser

const parseSVG = (template) => {
  var tmp = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
  tmp.innerHTML = template
  return tmp.children[0]
}

Throttle

μ—°μ†μ μœΌλ‘œ 콜백 μ‹€ν–‰ μš”μ²­λ˜μ–΄λ„, μ§€μ •λœ μ‹œκ°„ 주기둜 콜백이 μ‹€ν–‰ν•˜λŠ” 기법 Throttle 기법을 μ‚¬μš©ν•˜λ©΄ μΌμ •ν•œ μ‹œκ°„μ„ 주기둜 μ½œλ°±μ„ μ‹€ν–‰ ν•  수 μžˆλ‹€.

/**
 * Throttle
 *
 * @param callback {Function} Callback function
 * @param _threshhold {Number} Throttle time
 * @return {Function} Event Listener
 */
const throttle = (callback, ms = 100) => {
  let timer = null
  let last = 0

  return function (...args) {
    const self = this
    const now = +new Date

    if (last && now < last + ms){
      clearTimeout(timer)
      timer = setTimeout(() => {
        last = now
        callback.apply(self, args)
      }, ms)
    } else {
      last = now
      callback.apply(self, args)
    }
  }
}
const log = throttle(console.log, 1000)
setInterval(() => {
  console.log('------')
  log('1000')
  console.log('------')
}, 500)

// ------
// 1000
// ------
// ------
// ------
// ------
// 1000
// ------

Debounce

λ‹€μˆ˜μ˜ 이벀트 싀행을 λ°©μ§€ν•˜κΈ° μœ„ν•΄ ν•˜λ‚˜λ‘œ κ·Έλ£Ήν™”ν•˜μ—¬ νŠΉμ • μ‹œκ°„μ΄ μ§€λ‚œ ν›„ μ‹€ν–‰λ˜λŠ” 기법 μ—¬λŸ¬κ°œμ˜ μ΄λ²€νŠΈκ°€ κ³Όλ‹€ν•˜κ²Œ μ‚¬μš©μ΄ λ˜μ—ˆμ„ λ•Œ λΆ€ν•˜λ₯Ό μ€„μ—¬μ£ΌλŠ” 역할을 ν•œλ‹€.

/**
 * Debounce
 *
 * @param callback {Function} Callback function
 * @param _delay {Number} Delay time
 * @return {Function} Event Listener
 */
const debounce = (callback, ms) => {
  let timer = null

  return function(...args){
    const self = this
    clearTimeout(timer)
    timer = setTimeout(() => {
      callback.apply(self, args)
    }, ms)
  }
}
const log = debounce(console.log, 100)
log('1')
log('2')
log('3')
log('4')
// output: 4

Text More

.txt{
  width: 200px;
  max-height: 35px;
  line-height: 20px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal;
  word-wrap: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.txt.show{display:block;max-height:none}
.txt.show + .more{display:none}
<div class="wrap">
  <div class="txt">
    κ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆ
    κ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆκ°€λ‚˜λ‹€λΌλ§ˆ
  </div>
  <a href="#" class="more" onclick="toggle()">More</a>
</div>
const toggle = () => {
  const txt = document.querySelector('.txt').classList
  if (txt.contains('show')) {
    txt.remove('show')
  } else {
    txt.add('show')
  }
}
⚠️ **GitHub.com Fallback** ⚠️