其他分享
首页 > 其他分享> > 面试题:手写jquery要求考虑插件扩展性

面试题:手写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) {

  }
}

标签:面试题,const,插件,扩展性,selector,length,result,elem,fn
来源: https://www.cnblogs.com/kxsy/p/14866488.html