jQuery原理——20——jQuery的基本結構和入口函數測試

一: jQuery基本結構

1:jQuery的本質是一個閉包
2:jQuery爲什麼要用閉包來實現: 爲了避免多個框架的衝突
3:jQuery如何讓外界訪問內部定義的局部變量:window.xxx=xxx

(function test(){
     var num=1;
     window.num=num;
    })();
    console.log(num);

4:jQuery爲什麼要給自己傳遞一個 window參數?:爲了方便後期壓縮代碼,爲了提升找到的效率
5:jQuery爲什麼要給自己接收一個undefined參數? 爲了方便後期壓縮代碼,IE9以下的瀏覽器undefined可以被修改,爲了保證內部使用的undefined被修改,所以需要接受一個正確的undefined

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
    (function(window,undefined){
     var jQuery=function(){
         return new jQuery.prototype.init();
     }
     jQuery.prototype={
         constructor:jQuery
     }
     jQuery.prototype.init.prototype=jQuery.prototype;
     window.jQuery=window.$=jQuery;
    })(window);
   
    </script>
    
</head>
<body>
    
</body>
</html>

二:jQuery入口函數測試

jQ入口函數傳入不同參數得到的實例
1:傳入“ ” null undefined NaN 0 false

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../jQuery/jquery-3.4.1.min.js"></script>
    <script>
    $(function(){
        //會返回一個空的jQuery對象
        console.log($());
        console.log($(''));
        console.log($(null));
        console.log($(undefined));
        console.log($(NaN))
        console.log($(0))
        console.log($(false))
    })
    </script>
    
</head>
<body>
    
</body>
</html>

2:傳入html片段

<script>
    $(function(){
        //會將創建好的DOM元素存儲到jQuery
        console.log($('<p>1</p><p>2</p><p>3</p>'));
    })
    </script>

3:傳入選擇器

<script>
    $(function(){
        //會將找到的所有元素存儲到jQuery對象中
        console.log($('li'));
    })
    </script>

4:傳入數組

 <script>
 //會將數組中存儲的元素一次存儲到jQuery對象中返回
    $(function(){
        //會將找到的所有元素存儲到jQuery對象中
        console.log($([1,2,3,4,5,6]));
    })
    </script>

5:傳入僞數組

<script>
    $(function(){
        //會將數組中存儲的元素一次存儲到jQuery對象中返回
        var likeArr={0:'a',1:'b',length:2};
        console.log($(likeArr));
    })
    </script>

6:傳入對象

 <script>
    $(function(){
        //會將傳入的對象存儲到jQuery對象中返回
        function Person(){}
        console.log($(new Person()));
    })
    </script>

7:傳入DOM元素

 <script>
    $(function(){
        //會將傳入的DOM元素存儲到jQuery對象中返回
        function Person(){}
        console.log($(document.createElement('div')));
    })
    </script>

8:傳入基本數據類型

 <script>
    $(function(){
        //會將傳入的DOM元素存儲到jQuery對象中返回
        function Person(){}
        console.log($(document.createElement('div')));
    })
    </script>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章