jquery整体构架封装

  1. 如何全局使用jQuery
  2. 无new操作
  3. init原型
  4. 链式操作
<div id="dom">div</div>
    <!-- <script src="../jquery-3.3.1.js"></script> -->
    <script type="text/javascript">
        (function(){
             //全局使用jQuery
            window.jQuery = window.$ = jQuery;
            function jQuery(id){
                //无new操作
                return new init(id);
            }
            function init(id){
                var dom = document.getElementById(id.slice(1));
                this[0] = dom;
                this.length = 1;
                return this;
            }
            jQuery.prototype.text = function(){
                console.log('text');
                //链式操作
                return this;
            }
            jQuery.prototype.css = function(){
                console.log('css');
                //链式操作
                return this;
            }
            //init原型
            init.prototype = jQuery.prototype;
        }())
        console.log($('#dom').css('color','red').text());
        
    </script>
$作为函数/对象使用的方法

image.png
image.png

image.png

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容