面试题:手写jquery要求考虑插件扩展性

class Jquery {
  constructor(selector) {
    const result = document.querySelectorAll(selector)
    const length = result.length
    for(let i = 0; i < length; i++) {
      this[i] = result[i]
    }
    this.length = length
  }
  get(index) {
    return this[index]
  }
  each(fn) {
    for(let i = 0;i < this.length; i++) {
      const elem = this[i]
      fn(elem)
    }
  }
  on(type, fn) {
    return this.each(elem => {
      elem.addEventListener(type, fn, false)
    })
  }
}

// 插件
Jquery.prototype.dialog = function (info) {
  alert(info)
}

// 造*
class myJquery extends Jquery {
  constructor(selector) {
    super(selector)
  }
  addClass(className) {

  }
}

上一篇:阿里云kubernetes遭入侵pubg进程占用cpu资源100%解决方法


下一篇:线性表的顺序表示和实现