实现一个 jQuery 的 API的过程

今天要模仿jQuery来实现两个API,他们的功能是:

  1. 为选中的标签添加某个 class
  2. 将选中的标签的value替换为某个text

首先,顺着这个思路,写了以下代码:
html+css如下:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <style>
    .red {
      color: red;
    }
  </style>
</head>
<body>
<ul>
  <li>标签1</li>
  <li>标签2</li>
  <li>标签3</li>
  <li>标签4</li>
  <li>标签5</li>
</ul>
</body>
</html>

一. 想到哪写到哪
js代码如下:

    //给所有li添加类名red
    let liTags = document.querySelectorAll('ul > li')
    for(let i = 0;i<liTags.length;i++){
      liTags[i].classList.add('red')    
    }
    
    //把所有li的内容替换为hello
    for(let i = 0;i<liTags.length;i++){
      liTags[i].textContent = 'hello'   
    }

二. 封装为函数
上面的代码复用性很低,所以我们把他封装为函数,每次使用时调用就好

//给所有li添加类名red
    function addClass(selector,classes){
      let nodes = document.querySelectorAll(selector)
      for(let key in classes){
        let value = classes[key]
        if(value){
          for(let i = 0;i<nodes.length;i++){
            nodes[i].classList.add(key)  
          }
        }else {
          for(let i = 0;i<nodes.length;i++){
            nodes[i].classList.remove(key)  
          }
        }
      }
    }
        
    //把所有li的内容替换为hello
    function setText(selector,text){
      let nodes = document.querySelectorAll(selector)
      for(let i = 0;i<nodes.length;i++){
        nodes[i].textContent = text    
      }
    }
    let classes = {'red':true,'green':true,'blue':false}
    addClass.call(undefined,'ul>li',classes)
    setText.call(undefined,'ul>li','hello')

三. 添加命名空间
当我们自己添加的函数越来越多时,会不小心把很多全局变量都给覆盖了,所以需要命名空间

 window.myDom = {
      setClass: function(selector,classes){
        let nodes = document.querySelectorAll(selector)
        for(let key in classes){
          let value = classes[key]
          if(value){
            for(let i = 0;i<nodes.length;i++){
              nodes[i].classList.add(key)  
            }
          }else {
            for(let i = 0;i<nodes.length;i++){
              nodes[i].classList.remove(key)  
            }
          }
        }
      },
      setText: function(selector,text){
        let nodes = document.querySelectorAll(selector)
        for(let i = 0;i<nodes.length;i++){
          nodes[i].textContent = text    
        }
      }
    }
    myDom = window.myDom
   
    let classes = {'red':true,'green':true,'blue':false}
    myDom.setClass.call(undefined,'ul>li',classes)
    myDom.setText.call(undefined,'ul>li','hello')

四. 修改NodeList.prototype

NodeList.prototype.setClass = function(classes){
      for(let key in classes){
        let value = classes[key]
        if(value){
          for(let i = 0;i<this.length;i++){
            this[i].classList.add(key)  
          }
        }else {
          for(let i = 0;i<this.length;i++){
            this[i].classList.remove(key)  
          }
        }
      }
    }
    NodeList.prototype.setText = function(text){
      for(let i = 0;i<this.length;i++){
        this[i].textContent = text    
      }
    }
   
    let classes = {'red':true,'green':true,'blue':false}
    let liTags = document.querySelectorAll('ul>li')
    liTags.setClass.call(liTags,classes)
    liTags.setText.call(liTags,'hello')

五. 模仿jQuery

let jQuery = function(selector){
      let nodes = {}
      if(selector){
        nodes = document.querySelectorAll(selector)
      }
      nodes.setClass = function(classes){
        for(let key in classes){
          let value = classes[key]
          if(value){
            for(let i = 0;i<nodes.length;i++){
              nodes[i].classList.add(key)  
            }
          }else {
            for(let i = 0;i<nodes.length;i++){
              nodes[i].classList.remove(key)  
            }
          }
        }
      }
      nodes.setText = function(text){
        for(let i = 0;i<nodes.length;i++){
          nodes[i].textContent = text    
        }
      }
      return nodes
    }
    window.jQuery = jQuery
    window.$ = jQuery

   
    let classes = {'red':true,'green':true,'blue':false}
    let $liTags = $('ul>li')
    $liTags.setClass.call($liTags,classes)
    $liTags.setText.call($liTags,'hello')
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,950评论 1 45
  • 0. 写在前面 当你开始工作时,你不是在给你自己写代码,而是为后来人写代码。 —— Nichloas C. Zak...
    康斌阅读 5,405评论 1 42
  • 第3章 基本概念 3.1 语法 3.2 关键字和保留字 3.3 变量 3.4 数据类型 5种简单数据类型:Unde...
    RickCole阅读 5,286评论 0 21
  • 1、 jQuery 能做什么? jquery是一个丰富的js库,内部对js的很多复杂的方法进行了封装和加工,比如j...
    zh_yang阅读 1,435评论 6 13
  • 文章收录 展望:2018,属于数据驱动的智能媒体时代 站在2017,预测未来媒体 我的大编辑部设想 我的中央厨房建...
    sennchi阅读 208评论 0 0