01-jQuery的基本结构

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>01-jQuery的基本结构</title>

    <script>
        /* 
        1.jQuery的本质是一个闭包
        2.jQuery为什么要使用闭包来实现
            为了避免多个框架的冲突
        3.jQuery如何让外界访问内部定义的局部变量
            window.xxx = xxx;
        4.jQuery 为什么要给自己传递一个window参数?
            为了方便后期压缩代码
            为了提升查找的效率
        5. jQuery 为什么要给自己传递一个undefined参数?
            为了方便后期压缩代码
            IE9一下的浏览器undefined可以被修改,为了保证呢不适用的undefined不被修改,所以需要接受一个正确的undefined
    */
        (function (window, undefined) {
            jQuery = function(){
                return new jQuery.prototype.init();
            }

            jQuery.prototype = {
                constructor:jQuery
            }

            jQuery.prototype.init.prototype = jQuery.prototype;
            window.jQuery = window.$ = jQuery;
        })(window);


        (function f1() {
            var num = 10;
            window.num = num;
        })();

        (function f2() {
            var num = 20;
        })();

        console.log(num);


        var value = 20
        function f3(){
            var value = 10;
            console.log(value);
        }
    </script>
</head>

<body>

</body>

</html>

jQuery基本架构:

(function(window, undefined){
    var njQuery = function(){
        return new njQuery.prototype.init();
    }

    njQuery.prototype = {
        constructor: njQuery
    }

    njQuery.prototype.init.prototype = njQuery.prototype;
    window.njQuery = window.$ = njQuery;
})(window);

 

posted @ 2021-06-05 16:27  胸怀丶若谷  阅读(68)  评论(0编辑  收藏  举报