Skip to content
⚠️ This article was written in 2019. Some content may be outdated.

CSS 打印樣式表方案

CSS 打印樣式表方案是日常開發中經常遇到的問題。本文從實際項目出發,分享具體的實現方法和經驗總結。

基本概念

先來看基本的用法:

css
function deepClone(obj, map = new WeakMap()) {
  if (obj === null || typeof obj !== 'object') return obj
  if (map.has(obj)) return map.get(obj)

  const clone = Array.isArray(obj) ? [] : {}
  map.set(obj, clone)

  for (const key of Object.keys(obj)) {
    clone[key] = deepClone(obj[key], map)
  }
  return clone
}

這種寫法簡潔明瞭,適合大多數場景。

深入理解

核心代碼如下:

css
function pLimit(concurrency) {
  const queue = []
  let active = 0

  const next = () => {
    if (active >= concurrency || queue.length === 0) return
    active++
    const { fn, resolve, reject } = queue.shift()
    fn().then(resolve, reject).finally(() => {
      active--
      next()
    })
  }

  return (fn) => new Promise((resolve, reject) => {
    queue.push({ fn, resolve, reject })
    next()
  })
}

實際項目中還需要考慮邊界條件和異常處理。

項目應用

下面是一個實際的例子:

css
class EventEmitter {
  constructor() {
    this.events = new Map()
  }

  on(event, handler) {
    if (!this.events.has(event)) {
      this.events.set(event, [])
    }
    this.events.get(event).push(handler)
    return () => this.off(event, handler)
  }

  off(event, handler) {
    const handlers = this.events.get(event)
    if (handlers) {
      const idx = handlers.indexOf(handler)
      if (idx > -1) handlers.splice(idx, 1)
    }
  }

  emit(event, ...args) {
    const handlers = this.events.get(event) || []
    handlers.forEach(h => h(...args))
  }
}

這種模式在團隊中推廣後效果很好,維護成本明顯降低。

常見問題

我們可以通過以下方式實現:

css
interface User {
  id: number
  name: string
  email: string
  role: 'admin' | 'user' | 'guest'
}

function createUser(data: Partial<User>): User {
  return {
    id: Date.now(),
    name: data.name || '',
    email: data.email || '',
    role: data.role || 'user'
  }
}

type UserKeys = keyof User  // 'id' | 'name' | 'email' | 'role'

注意上面代碼中的性能細節,避免不必要的計算。

小結

  • CSS 打印樣式表方案的關鍵在於理解核心概念,不要停留在表面用法
  • 實際項目中根據場景選擇合適的方案
  • 團隊中統一約定比追求完美實現更重要
  • 持續學習和總結,保持技術敏感度

MIT Licensed