初步实现 jQuery 思想的细节记录

window.jQuery = function (nodeOrSelector){ //在全局环境下创建 jQuery 对象,创建之初想到节点有可能是字符串的情况
    let nodes = {}                        //创建一个容纳所有符合条件的节点纯对象组
    if (typeof nodeOrSelector === 'string') {  //判断传入的节点类型是否为字符串
        let temp = document.querySelector(nodeOrSelector)    //如果为字符串,则获取所有符合条件的节点,返回一个伪数组
        for(let i = 0;i< temp.length;i++){    //遍历这个得到的伪数组
            nodes[i] = temp[i]    //  将伪数组内的内容传给空对象 nodes
    }
    nodes.length = temp.length     // 传递完毕,将伪数组内的 length 属性的 key 及value传给 nodes 对象
    } else if(nodeOrSelector instanceof Node){    //如果函数传入的参数确为节点
            nodes = {                             //将参数传给 nodes 对象的第一个属性‘0’中,同时设定对象内的 length 值为1
                0:nodeOrSelector,
                length:1
            }
    }
  
    nodes.addClass:function(value){            //为 nodes 对象设定方法‘addClass’,方法参数为 value
          for(let i=0; i

你可能感兴趣的:(初步实现 jQuery 思想的细节记录)