轻量级模拟jQuery框架封装

jQuery 轻量级jQuery代码分析

已经实现的功能: each方法,map方法,toArray方法,get方法;

(function (window) {
        function MHQ(selector) { 
           return new MHQ.fn.init(selector);
        }        
MHQ.fn = MHQ.prototype = {            
      constructor: MHQ,            
      type: "MHQ",
      init: function (selector) { 
      // 判断传入的选择器类型,如果什么都不传,什么都不做 
               if (!selector) return this; 
               // 判断传入的是否是string类型
                if (typeof selector === "string") { 
                   // 判断是不是包含HTML标签的string类型
                    if (selector.charAt(0) === "<") {
                    } else { // 是选择器
                        [].push.apply(this, document.querySelectorAll(selector));                        return this; 
                   }
                } else if (selector.constructor.name = MHQ) { // 判断传入的是否是MHQ类型
                } else if (selector.nodeType) { // 判断是否是dom对象
                } else if (typeof selector === "function") { // 判断是否是function
                } 
             }
          };
          MHQ.fn.init.prototype = MHQ.fn; 
         // 此时使用 Itcast.fn.extend 扩展实例成员 
         // 使用 Itcast.extend 扩展静态成员
         MHQ.extend = MHQ.fn.extend = function (obj) {
            var k;
            for (k in obj) {
                this[k] = obj[k];
            }
        };
        MHQ.fn.extend({
            each: function (callback) {
                return MHQ.each(this, callback);
            },
            map: function (callback) {
                return MHQ.map(this, callback);
            }
        });
        MHQ.extend({
            each: function (obj, callback) {
                var i = 0,
                        isArray = obj.length >= 0;
                if (isArray) {
                    for (; i < obj.length; i++) {
                        if (callback.call(obj[i], i, obj[i]) === false) {
                            break;
                        }
                    }
                } else {
                    for (i in obj) {
                        if (callback.call(obj[i], i, obj[i]) === false) {
                            break;
                        }
                    }
                }
                return obj;
            },
            map: function (obj, callback) {
                var i = 0,
                        isArray = obj.length >= 0,
                        rect = [],
                        result;
                if (isArray) {
                    for (; i < obj.length; i++) {
                        result = callback(obj[i], i);
                        if (result != null) {
                            return rect.push(result);
                        }
                    }
                } else {
                    for (i in obj) {
                        result = callback(obj[i], i);
                        if (result != null) {
                            return rect.push(result);
                        }
                    }
                } 
               return rect;
            }        
});        
        // 核心功能
        MHQ.fn.extend({
            toArray: function () {
                return [].slice.call(this);
            },
            get: function (index) {
                if (index === undefined) {
                    return this.toArray();
                } else {
                    return this[index > 0 ? index : this.length + index];
                }
            }
        });
        window.MHQ = window.M = MHQ;
        // DOM模块
})(window);
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>01.轻量级jQuery框架搭建-实现基本功能</title>
    <script src="MHQ.js"></script>
</head>
<body>
    <div>1</div>
    <div>3</div>
    <div>2</div>
    <div>4</div>
</body>
<script>
    var arr = M("div").get();arr.forEach(function (v) {
        v.style.width = "500px";
        v.style.height = "50px";
        v.style.margin = "10px 0";
        v.style.border = "1px dashed #0088AA";
    });
    M( 'div' ).get( 1 ).style.backgroundColor = 'pink';
</script>
</html>
测试.png

写在最后,版本持续更改中...

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文学习jQuery源码,封装了each、map、toArray、get以及,一些基本DOM操作方法 博客原文地址...
    育儿与心理阅读 1,482评论 1 1
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,797评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,179评论 18 399
  • 1.JQuery 基础 改变web开发人员创造搞交互性界面的方式。设计者无需花费时间纠缠JS复杂的高级特性。 1....
    LaBaby_阅读 5,242评论 0 2
  • *内存管理,是指软件运行时对计算机内存资源的分配和使用的技术。其最主要的目的是如何高效,快速的分配,并且在适当的时...
    青珩阅读 1,449评论 0 0