1. class jQuery {
    2. constructor(selector) {
    3. const result = document.querySelectorAll(selector)
    4. const length = result.length
    5. for (let i = 0; i < length; i++) {
    6. this[i] = result[i]
    7. this.length = length
    8. this.selector = selector
    9. }
    10. }
    11. get(index) {
    12. return this[index]
    13. }
    14. each(fn) {
    15. for(let i = 0; i < this.length; i++) {
    16. const elem = this[i]
    17. fn(elem)
    18. }
    19. }
    20. on(type, fn) {
    21. return this.each(elem => {
    22. elem.addEventListener(type, fn, false)
    23. })
    24. }
    25. }
    26. // 插件
    27. jQuery.prototype.dialog = function (info) {
    28. alert(info)
    29. }
    30. // 造轮子
    31. class myJQuery extends jQuery {
    32. constructor(selector) {
    33. super(selector)
    34. }
    35. // 扩展自己的方法
    36. style(data) {
    37. }
    38. }
    39. const $p = new jQuery('p')
    40. $p.get(1)