• 面试:封装DOM


    目录

    架构:

    封装添加class 和修改text 的方法:写在$ 函数下面

    优化:第二个版本

    for换成each优化的点:

    第三个版本:


    架构:

    匿名自执行函数:

    ( function( window ){

            window.$ = jquery = function(nodeSelector){

                 

            }

    }) (window)

    好处:

    1. 自执行 -> 单例模式
    2. 防止变量污染

    封装添加class 和修改text 的方法:写在$ 函数下面

    1. (function(window) {
    2. window.$ = jquery = function(nodeSelector) {
    3. let nodes = {};
    4. if (typeof nodeSelector === "string") {
    5. let temp = document.querySelectorAll(nodeSelector)
    6. for (let i in temp) nodes[i] = temp[i]
    7. //temp 伪数组,nodes 类数组
    8. nodes.length = temp.length
    9. } else {
    10. throw new Error('必须输入字符串')
    11. }
    12. //添加方法
    13. nodes.addClass = function(classes) {
    14. let className = classes.split(' ');
    15. //循环class
    16. className.forEach(value => {
    17. for (let i = 0; i < nodes.length; i++) {
    18. nodes[i].classList.add(value)
    19. }
    20. })
    21. }
    22. // 修改 text
    23. nodes.setText = function(text) {
    24. for (let key of nodes) {
    25. key.textContent = text
    26. }
    27. }
    28. return nodes
    29. }
    30. })(window)

    优化:第二个版本

    call :语法 call(obj,p1,p2) 

    obj 是更改之后 this 指向的对象(必选),p1,p2是原始函数要传的参数(可选)

    dom2.js 代码:

    let $ = jQuery = (function(window){

            let jquery = function(nodeSelector){

                    //真数组

                    this.nodes = document.querySeletorAll(nodeSelector)

            }

            //原型方法包装

            jquery.prototype = {

                    each: function(callback){

                           for (let i = 0; i < nodes.length; i++) {

                                    callback.call(this, i, this.nodes[i])

                            }

                    },

                    //添加方法
                    addClass:  function(classes){

                            let className = classes.split(' ');

                            className.forEach(value => {

                                   this.each(function(index, obj){ obj.ClassList.add(value) })

                            })

                    }

            }

            return function(nodeSelector){

                    return new jquery(nodeSelector)

            }

    })()

    1. let $ = jQuery = (function(window) {
    2. let jquery = function(nodeSelector) {
    3. //真数组
    4. this.nodes = document.querySelectorAll(nodeSelector)
    5. }
    6. //原型方法包装
    7. jquery.prototype = {
    8. each: function(callback) {
    9. for (let i = 0; i < nodes.length; i++) {
    10. callback.call(this, i, this.nodes[i])
    11. }
    12. },
    13. //添加方法
    14. addClass: function(classes) {
    15. let className = classes.split(' ');
    16. className.forEach(value => {
    17. this.each(function(index, obj) {
    18. obj.classList.add(value)
    19. })
    20. })
    21. }
    22. }
    23. return function(nodeSelector) {
    24. return new jquery(nodeSelector)
    25. }
    26. })()

    for换成each优化的点:

    将每个相似又独立的for循环,抽取出来,在架构上更加便于管理

    第三个版本:

    1. let $ = jQuery = (function(window){
    2. //dom 存储
    3. function Query(dom, selector){
    4. let i, len = dom ? dom.length : 0
    5. for(i = 0; i < len; i++) this[i] = dom[i]
    6. this.length = len
    7. this.selector = selector || ''
    8. return this
    9. }
    10. //生成 jQuery 对象
    11. function Z(elements, selector){
    12. return Query.call(this, elements, selector)
    13. }
    14. //具体的 dom 查找
    15. function qsa(element, selector){
    16. return element.querySelectorAll(selector)
    17. }
    18. Z.prototype = {
    19. //中央集权
    20. each(callback){
    21. [].every.call(this, function(el, index){
    22. return callback.call(el, index, el) !== false
    23. })
    24. },
    25. //查找的方法
    26. find(selector){
    27. let doms = []
    28. this.each(function(index, el){
    29. let childs = this.querySelectorAll(selector)
    30. doms.push(...childs)
    31. })
    32. return new Z(doms, selector)
    33. },
    34. eq(i){
    35. let doms = []
    36. this.each(function(index, el){
    37. if(i == index) doms.push(this)
    38. })
    39. return new Z(doms, this.selector)
    40. },
    41. remove(){
    42. this.each(function(index, el){
    43. this.remove()
    44. })
    45. }
    46. }
    47. //全局方法
    48. function isFunction(value) { return typeof value == 'function' }
    49. function $(nodeSelector){
    50. let doms = qsa(document, nodeSelector)
    51. return new Z(doms, nodeSelector)
    52. }
    53. $.isFunction = isFunction;
    54. return $
    55. })(window)

  • 相关阅读:
    Linunx搜索,查找类
    ArrayList 和 LinkedList 的区别
    区块链 | ERC721 标准
    用户增长模型:3A3R策略模型
    MAC地址注册的网络安全影响和措施分析
    超级强大!送你几款Linux 下终极SSH客户端
    JAVA:实现DynamicArray动态数组算法(附完整源码)
    深入理解常见的二十三种设计模式
    6.3 Case Studies - PIMPL
    [一周AI简讯]OpenAI宫斗;微软Bing Chat更名Copilot;Youtube测试音乐AI
  • 原文地址:https://blog.csdn.net/qq_52179917/article/details/125448725