cainiaoblog 23.2.171
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CNAME +1 -0
- package/MP_verify_o6dHCK5mIkxNpzvZ.txt +1 -0
- package/README.md +8 -0
- package/about/index.html +54 -0
- package/ads.txt +1 -0
- package/app/manual.html +104 -0
- package/appConfigs/config +1 -0
- package/archives/2018/index.html +48 -0
- package/archives/2019/index.html +48 -0
- package/archives/2019/page/2/index.html +48 -0
- package/archives/2020/index.html +48 -0
- package/archives/2021/index.html +48 -0
- package/archives/2022/index.html +48 -0
- package/archives/2023/index.html +48 -0
- package/archives/index.html +48 -0
- package/archives/page/2/index.html +48 -0
- package/archives/page/3/index.html +48 -0
- package/article/10047.html +148 -0
- package/article/10164.html +64 -0
- package/article/10624.html +436 -0
- package/article/10745.html +586 -0
- package/article/11674.html +71 -0
- package/article/11939.html +72 -0
- package/article/12238.html +86 -0
- package/article/1230.html +533 -0
- package/article/13265.html +69 -0
- package/article/13823.html +78 -0
- package/article/14125.html +72 -0
- package/article/14192.html +561 -0
- package/article/14375.html +85 -0
- package/article/14744.html +83 -0
- package/article/14936.html +451 -0
- package/article/1518.html +250 -0
- package/article/15428.html +71 -0
- package/article/15518.html +288 -0
- package/article/15569.html +418 -0
- package/article/1566.html +122 -0
- package/article/16017.html +156 -0
- package/article/16947.html +440 -0
- package/article/17096.html +82 -0
- package/article/17246.html +89 -0
- package/article/17575.html +350 -0
- package/article/179.html +757 -0
- package/article/18039.html +233 -0
- package/article/18071.html +375 -0
- package/article/18142.html +71 -0
- package/article/19909.html +160 -0
- package/article/20014.html +89 -0
- package/article/20021.html +389 -0
- package/article/20037.html +280 -0
- package/article/20056.html +95 -0
- package/article/20093.html +404 -0
- package/article/2021.html +607 -0
- package/article/20459.html +115 -0
- package/article/20613.html +263 -0
- package/article/20635.html +71 -0
- package/article/21317.html +71 -0
- package/article/21320.html +90 -0
- package/article/21782.html +548 -0
- package/article/22230.html +435 -0
- package/article/22237.html +347 -0
- package/article/22398.html +109 -0
- package/article/22422.html +270 -0
- package/article/22565.html +300 -0
- package/article/23046.html +250 -0
- package/article/23083.html +215 -0
- package/article/23195.html +297 -0
- package/article/23301.html +835 -0
- package/article/23589.html +285 -0
- package/article/23655.html +338 -0
- package/article/23864.html +76 -0
- package/article/23914.html +96 -0
- package/article/24169.html +135 -0
- package/article/24581.html +349 -0
- package/article/2475.html +500 -0
- package/article/24826.html +77 -0
- package/article/24924.html +247 -0
- package/article/25164.html +1495 -0
- package/article/25318.html +394 -0
- package/article/25710.html +1129 -0
- package/article/25734.html +83 -0
- package/article/26083.html +174 -0
- package/article/26147.html +283 -0
- package/article/26291.html +110 -0
- package/article/26354.html +71 -0
- package/article/26624.html +145 -0
- package/article/26639.html +298 -0
- package/article/26850.html +138 -0
- package/article/26940.html +258 -0
- package/article/27796.html +190 -0
- package/article/27800.html +161 -0
- package/article/28963.html +159 -0
- package/article/28976.html +247 -0
- package/article/29042.html +72 -0
- package/article/29596.html +81 -0
- package/article/29619.html +183 -0
- package/article/29623.html +329 -0
- package/article/29829.html +205 -0
- package/article/29abbd1c.html +112 -0
- package/article/30215.html +178 -0
- package/article/30552.html +78 -0
- package/article/30818.html +201 -0
- package/article/31330.html +364 -0
- package/article/3154.html +577 -0
- package/article/31695.html +348 -0
- package/article/31826.html +71 -0
- package/article/31843.html +132 -0
- package/article/32280.html +212 -0
- package/article/32431.html +74 -0
- package/article/32443.html +72 -0
- package/article/32732.html +122 -0
- package/article/3286.html +251 -0
- package/article/32960.html +198 -0
- package/article/33713.html +72 -0
- package/article/33899.html +615 -0
- package/article/34351.html +74 -0
- package/article/34372.html +89 -0
- package/article/34410.html +74 -0
- package/article/34431.html +118 -0
- package/article/3447.html +75 -0
- package/article/34474.html +142 -0
- package/article/34871.html +147 -0
- package/article/35397.html +175 -0
- package/article/3544.html +199 -0
- package/article/35503.html +115 -0
- package/article/35624.html +84 -0
- package/article/35697.html +384 -0
- package/article/35863.html +73 -0
- package/article/36037.html +253 -0
- package/article/36118.html +71 -0
- package/article/3621.html +119 -0
- package/article/36251.html +296 -0
- package/article/36642.html +352 -0
- package/article/37102.html +763 -0
- package/article/37336.html +351 -0
- package/article/37351.html +89 -0
- package/article/3760.html +294 -0
- package/article/37749.html +75 -0
- package/article/37789.html +83 -0
- package/article/38005.html +765 -0
- package/article/38009.html +284 -0
- package/article/38269.html +194 -0
- package/article/38344.html +88 -0
- package/article/38359.html +334 -0
- package/article/38423.html +379 -0
- package/article/38530.html +213 -0
- package/article/38743.html +268 -0
- package/article/38753.html +204 -0
- package/article/38881.html +71 -0
- package/article/39087.html +414 -0
- package/article/39309.html +84 -0
- package/article/39486.html +74 -0
- package/article/39637.html +306 -0
- package/article/39839.html +72 -0
- package/article/39937.html +152 -0
- package/article/39970.html +71 -0
- package/article/402.html +559 -0
- package/article/40698.html +71 -0
- package/article/40811.html +71 -0
- package/article/40964.html +462 -0
- package/article/41011.html +322 -0
- package/article/41152.html +78 -0
- package/article/41696.html +71 -0
- package/article/41717.html +281 -0
- package/article/41777.html +149 -0
- package/article/419.html +985 -0
- package/article/4233.html +136 -0
- package/article/4292.html +86 -0
- package/article/43302.html +96 -0
- package/article/43397.html +106 -0
- package/article/43724.html +281 -0
- package/article/43999.html +224 -0
- package/article/44426.html +77 -0
- package/article/44964.html +137 -0
- package/article/45004.html +123 -0
- package/article/4510.html +83 -0
- package/article/45156.html +320 -0
- package/article/45157.html +52 -0
- package/article/45267.html +87 -0
- package/article/45641.html +207 -0
- package/article/45960.html +75 -0
- package/article/46100.html +1262 -0
- package/article/46366.html +74 -0
- package/article/46771.html +96 -0
- package/article/46911.html +517 -0
- package/article/46948.html +137 -0
- package/article/46ee4850.html +349 -0
- package/article/47019.html +84 -0
- package/article/47532.html +94 -0
- package/article/4783.html +429 -0
- package/article/47972.html +72 -0
- package/article/48216.html +127 -0
- package/article/48252.html +185 -0
- package/article/48520.html +74 -0
- package/article/48722.html +75 -0
- package/article/48807.html +99 -0
- package/article/49126.html +323 -0
- package/article/50001.html +88 -0
- package/article/50058.html +151 -0
- package/article/50343.html +410 -0
- package/article/50510.html +82 -0
- package/article/51082.html +98 -0
- package/article/51677.html +111 -0
- package/article/5204.html +1085 -0
- package/article/52500.html +538 -0
- package/article/52689.html +259 -0
- package/article/52877.html +569 -0
- package/article/53125.html +191 -0
- package/article/53428.html +251 -0
- package/article/53524.html +74 -0
- package/article/53698.html +322 -0
- package/article/53803e6.html +93 -0
- package/article/53924.html +291 -0
- package/article/53962.html +253 -0
- package/article/54006.html +71 -0
- package/article/54018.html +88 -0
- package/article/54161.html +83 -0
- package/article/54178.html +69 -0
- package/article/54258.html +327 -0
- package/article/54295.html +214 -0
- package/article/54313.html +1976 -0
- package/article/54351.html +90 -0
- package/article/54604.html +80 -0
- package/article/54632.html +71 -0
- package/article/54912.html +756 -0
- package/article/54918.html +129 -0
- package/article/55119.html +136 -0
- package/article/55756.html +118 -0
- package/article/55769.html +136 -0
- package/article/55870.html +161 -0
- package/article/56139.html +92 -0
- package/article/56557.html +69 -0
- package/article/56604.html +69 -0
- package/article/56816.html +266 -0
- package/article/56926.html +153 -0
- package/article/57747.html +409 -0
- package/article/57965.html +475 -0
- package/article/58816.html +78 -0
- package/article/59059.html +435 -0
- package/article/59095.html +350 -0
- package/article/59349.html +244 -0
- package/article/59402.html +216 -0
- package/article/59448.html +55 -0
- package/article/59870.html +94 -0
- package/article/59877.html +208 -0
- package/article/5dbb6f41.html +86 -0
- package/article/6008.html +75 -0
- package/article/60327.html +126 -0
- package/article/6035.html +76 -0
- package/article/60428.html +71 -0
- package/article/60544.html +128 -0
- package/article/61319.html +95 -0
- package/article/61367.html +93 -0
- package/article/624d1918.html +292 -0
- package/article/62543.html +71 -0
- package/article/62887.html +169 -0
- package/article/63074.html +120 -0
- package/article/63266.html +311 -0
- package/article/63351.html +74 -0
- package/article/63542.html +104 -0
- package/article/63824.html +275 -0
- package/article/64051.html +653 -0
- package/article/64189b69.html +72 -0
- package/article/64354.html +75 -0
- package/article/64869.html +79 -0
- package/article/64923.html +596 -0
- package/article/65128.html +95 -0
- package/article/65295.html +79 -0
- package/article/6888.html +75 -0
- package/article/7065.html +84 -0
- package/article/7455.html +85 -0
- package/article/7a3ef8f.html +315 -0
- package/article/8140.html +188 -0
- package/article/8190.html +85 -0
- package/article/8225.html +78 -0
- package/article/84c9ccd6.html +159 -0
- package/article/8f9a9ae3.html +82 -0
- package/article/9174.html +73 -0
- package/article/9738.html +294 -0
- package/article/a4145266.html +71 -0
- package/article/bedea419.html +72 -0
- package/article/bf7e7421.html +78 -0
- package/article/c1867fbf.html +103 -0
- package/article/c5576490.html +70 -0
- package/article/dcb0db28.html +76 -0
- package/article/fc3b727a.html +79 -0
- package/atom.xml +445 -0
- package/baidusitemap.xml +1079 -0
- package/blocks/dns.txt +264 -0
- package/blocks/filter.txt +1070 -0
- package/blocks/script.js +42 -0
- package/categories/CDN/index.html +48 -0
- package/categories/IDE/index.html +48 -0
- package/categories/git/index.html +48 -0
- package/categories/hexo/index.html +48 -0
- package/categories//345/205/254/345/205/261/350/212/202/346/227/245/index.html +48 -0
- package/categories//345/210/206/344/272/253/index.html +48 -0
- package/categories//345/211/215/347/253/257/index.html +48 -0
- package/categories//345/211/215/347/253/257/page/2/index.html +48 -0
- package/categories//345/211/215/347/253/257//346/265/217/350/247/210/345/231/250/index.html +48 -0
- package/categories//345/211/215/347/253/257//346/265/217/350/247/210/345/231/250//347/275/221/347/273/234/345/215/217/350/256/256/index.html +48 -0
- package/categories//345/215/232/345/256/242/index.html +48 -0
- package/categories//345/267/245/345/205/267/index.html +48 -0
- package/categories//345/271/277/345/221/212/350/277/207/346/273/244/index.html +48 -0
- package/categories//345/271/277/345/221/212/350/277/207/346/273/244//350/247/206/351/242/221/347/224/265/350/247/206/345/260/217/350/257/264/346/274/253/347/224/273/350/265/204/350/256/257/351/237/263/344/271/220/index.html +48 -0
- package/categories//346/255/243/345/210/231/index.html +48 -0
- package/categories//346/265/217/350/247/210/345/231/250/index.html +48 -0
- package/categories//347/256/227/346/263/225/index.html +48 -0
- package/categories//350/265/204/350/256/257/index.html +48 -0
- package/categories//350/275/273/346/235/276/344/270/200/345/210/273/index.html +48 -0
- package/cdn/css/font-awesome.min.css +4 -0
- package/cdn/css/grids-responsive-min.css +7 -0
- package/cdn/css/normalize.css +349 -0
- package/cdn/css/pure-min.css +11 -0
- package/cdn/fonts/fontawesome-webfont.eot +0 -0
- package/cdn/fonts/fontawesome-webfont.svg +2671 -0
- package/cdn/fonts/fontawesome-webfont.ttf +0 -0
- package/cdn/fonts/fontawesome-webfont.woff +0 -0
- package/cdn/fonts/fontawesome-webfont.woff2 +0 -0
- package/cdn/js/Valine.min.js +17 -0
- package/cdn/js/canvas-nest.js +1 -0
- package/cdn/js/clipboard.min.js +7 -0
- package/cdn/js/crypto-js.js +25 -0
- package/cdn/js/jquery.min.js +2 -0
- package/css/default.css +1144 -0
- package/css/donate.css +338 -0
- package/css/hbe.style.css +749 -0
- package/css/style.css +2095 -0
- package/donate/index.html +39 -0
- package/google917dcf72f6e5c7f5.html +1 -0
- package/guestbook/index.html +68 -0
- package/hosts/ads.txt +149 -0
- package/hosts/flash.txt +9 -0
- package/hosts/github.txt +6 -0
- package/hosts/google.txt +16936 -0
- package/hosts/winrar.txt +2 -0
- package/img/alipay.svg +46 -0
- package/img/bitcoin.svg +135 -0
- package/img/blog.ico +0 -0
- package/img/blog.png +0 -0
- package/img/gaba.png +0 -0
- package/img/github.svg +1 -0
- package/img/like.svg +1 -0
- package/img/mp-mini.jpg +0 -0
- package/img/mp.png +0 -0
- package/img/paypal.svg +63 -0
- package/img/upy_logo.svg +59 -0
- package/img/wechat.svg +49 -0
- package/index.html +48 -0
- package/jd_root.txt +1 -0
- package/js/codeblock-resizer.js +51 -0
- package/js/dark.js +12 -0
- package/js/donate.js +87 -0
- package/js/fixedPage.js +106 -0
- package/js/gitment.browser.js +3751 -0
- package/js/search.js +86 -0
- package/js/share.js +60 -0
- package/js/smartresize.js +32 -0
- package/js/totop.js +12 -0
- package/kankan/index.html +77 -0
- package/lib/hbe.js +293 -0
- package/package.json +25 -0
- package/page/10/index.html +49 -0
- package/page/11/index.html +59 -0
- package/page/12/index.html +67 -0
- package/page/13/index.html +63 -0
- package/page/14/index.html +66 -0
- package/page/15/index.html +60 -0
- package/page/16/index.html +60 -0
- package/page/17/index.html +62 -0
- package/page/18/index.html +61 -0
- package/page/19/index.html +58 -0
- package/page/2/index.html +48 -0
- package/page/20/index.html +57 -0
- package/page/21/index.html +61 -0
- package/page/22/index.html +55 -0
- package/page/23/index.html +64 -0
- package/page/24/index.html +60 -0
- package/page/25/index.html +58 -0
- package/page/26/index.html +48 -0
- package/page/27/index.html +58 -0
- package/page/3/index.html +48 -0
- package/page/4/index.html +48 -0
- package/page/5/index.html +51 -0
- package/page/6/index.html +51 -0
- package/page/7/index.html +51 -0
- package/page/8/index.html +53 -0
- package/page/9/index.html +48 -0
- package/randomcall/README.html +1 -0
- package/randomcall/lucker.html +290 -0
- package/randomcall/lucky.png +0 -0
- package/robots.txt +22 -0
- package/search.xml +6740 -0
- package/sitemap.xml +2669 -0
- package/tags//345/216/237/345/210/233/index.html +48 -0
- package/tags//345/250/261/344/271/220/index.html +48 -0
- package/tags//346/211/213/345/206/231/index.html +48 -0
- package/tags//347/226/253/346/203/205/index.html +48 -0
- package/tags//350/275/254/350/275/275/index.html +48 -0
- package/tags//350/275/254/350/275/275/page/2/index.html +48 -0
- package/v.html +90 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
<!DOCTYPE html><html lang="zh-CN" data-dark><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"><meta content="yes" name="apple-mobile-web-app-capable"><meta content="black-translucent" name="apple-mobile-web-app-status-bar-style"><meta content="telephone=no" name="format-detection"><meta name="description" content="前端壹菜鸟,记录工作难题,关注前端知识,收集精彩博文,做技术的搬运工"><meta name="msvalidate.01" content="0FE4D8B3381D3D87088996B886E1E2BD"><title>Webpack 是怎样运行的? | 前端壹菜鸟</title><link rel="stylesheet" type="text/css" href="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/css/style.min.css"><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/normalize.css@latest/normalize.min.css"><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/purecss@latest/build/pure-min.min.css"><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/purecss@latest/build/grids-responsive-min.css"><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css"><link rel="icon" mask="" sizes="any" href="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/blog.ico"><link rel="Shortcut Icon" type="image/x-icon" href="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/blog.ico"><link rel="apple-touch-icon" href="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/blog.png"><link rel="apple-touch-icon-precomposed" href="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/blog.png"><link rel="alternate" type="application/atom+xml" href="/atom.xml"><script type="text/javascript" src="//cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8385136408348258"></script><script src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script><script>(function(){
|
|
2
|
+
var bp = document.createElement('script');
|
|
3
|
+
var curProtocol = window.location.protocol.split(':')[0];
|
|
4
|
+
if (curProtocol === 'https'){
|
|
5
|
+
bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
|
|
6
|
+
}
|
|
7
|
+
else{
|
|
8
|
+
bp.src = 'http://push.zhanzhang.baidu.com/push.js';
|
|
9
|
+
}
|
|
10
|
+
var s = document.getElementsByTagName("script")[0];
|
|
11
|
+
s.parentNode.insertBefore(bp, s);
|
|
12
|
+
})();
|
|
13
|
+
</script><script>(function(){
|
|
14
|
+
var src = 'https://jspassport.ssl.qhimg.com/11.0.1.js?d182b3f28525f2db83acfaaf6e696dba';
|
|
15
|
+
document.write('<script src="' + src + '" id="sozz"><\/script>');
|
|
16
|
+
})();
|
|
17
|
+
</script><script>var _hmt=_hmt||[];(function(){var hm=document.createElement("script");hm.src='https://hm.baidu.com/hm.js?011f0b44e8452bfa57fbfa23c5fe7683';var s=document.getElementsByTagName("script")[0];s.parentNode.insertBefore(hm, s);})();
|
|
18
|
+
</script><script async src="https://www.googletagmanager.com/gtag/js?id=UA-150860401-1"></script><script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);};gtag('js',new Date());gtag('config','UA-150860401-1');
|
|
19
|
+
</script><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/fixedPage.min.js"></script><meta name="generator" content="Hexo 6.3.0"></head><body><div class="body_container"><div id="header"><div class="site-name"><a id="logo" href="/.">前端壹菜鸟</a><p class="description">关注前端知识,收集精彩博文,做技术的搬运工</p></div><div id="nav-menu"><a class="current" href="/." target="_self"><i class="fa fa-home"> 首页</i></a><a href="/archives/" target="_self"><i class="fa fa-archive"> 归档</i></a><a href="/guestbook/" target="_self"><i class="fa fa-comments"> 留言</i></a><a href="/about/" target="_self"><i class="fa fa-user"> 关于</i></a><a href="https://laonongmin.online/" target="_target"><i class="fa fa-group"> 看看</i></a><a href="https://love.xuehuayu.cn/" target="_target"><i class="fa fa-heart"> LOVE</i></a></div></div><div class="pure-g" id="layout"><div class="pure-u-1 pure-u-md-3-4"><div class="content_container"><div class="post"><h1 class="post-title c-post">Webpack 是怎样运行的?</h1><div class="post-meta"><span class="date">2019-12-02</span><span> | </span><span class="tag"><a href="/tags/%E8%BD%AC%E8%BD%BD/">转载 </a></span><span> | </span><span class="category"><a href="/categories/%E5%89%8D%E7%AB%AF/">前端 </a></span><span id="busuanzi_container_page_pv"> | <span id="busuanzi_value_page_pv"><i class="fa fa-spinner"></i></span><span> 阅读</span></span><span class="post-time"><span class="post-meta-item-text"> | </span><span class="post-meta-item-icon"><i class="fa fa-keyboard-o"></i><span class="post-count"> 2.1k</span><span class="post-meta-item-text"> 字</span></span></span><span class="post-time"> | <span class="post-meta-item-icon"><i class="fa fa-hourglass-half"></i><span class="post-count"> 8</span><span class="post-meta-item-text"> 分钟</span></span></span></div><a class="disqus-comment-count" href="/article/38530.html#vcomment"><span class="waline-comment-count" data-path="/article/38530.html"></span><span> 条评论</span></a><div class="post-content"><p><code>原文地址:https://zhuanlan.zhihu.com/p/52826586</code></p>
|
|
20
|
+
<p>Webpack 是时下最流行的前端打包工具,它打包开发代码,输出能在各种浏览器运行的代码,提升了开发至发布过程的效率。</p>
|
|
21
|
+
<p>你可能已经知道,这种便捷是由 Webpack 的插件系统带来的,但我们今天先把这些概念放在一边,从简单的实践开始,探索 Webpack 打包出的代码是如何在浏览器环境运行的。</p>
|
|
22
|
+
<span id="more"></span>
|
|
23
|
+
|
|
24
|
+
<h2 id="简单配置"><a href="#简单配置" class="headerlink" title="简单配置"></a>简单配置</h2><p>配置文件是使用 Webpack 的关键,一份配置文件主要包含入口(entry)、输出文件(output)、模式、加载器(Loader)、插件(Plugin)等几个部分,但如果只需要组织 JS 文件的话,指定入口和输出文件路径即可完成一个迷你项目的打包:</p>
|
|
25
|
+
<p>项目目录:</p>
|
|
26
|
+
<ul>
|
|
27
|
+
<li><p>build</p>
|
|
28
|
+
</li>
|
|
29
|
+
<li><p>webpack.config.js – 存放 webpack 配置对象</p>
|
|
30
|
+
</li>
|
|
31
|
+
<li><p>src</p>
|
|
32
|
+
</li>
|
|
33
|
+
<li><p>index.js – 源文件</p>
|
|
34
|
+
</li>
|
|
35
|
+
<li><p>package.json – 本文使用 webpack ^4.23.0 作示例</p>
|
|
36
|
+
</li>
|
|
37
|
+
</ul>
|
|
38
|
+
<p>为了更好地观察产出的文件,我们将模式设置为 <code>development</code> 关闭代码压缩,再开启 <code>source-map</code> 支持原始源代码调试。</p>
|
|
39
|
+
<p>配置文件 <code>build/webpack.config.js</code>:</p>
|
|
40
|
+
<pre><code>constpath=require('path');constresolve=relativePath=>path.resolve(__dirname,relativePath);module.exports={mode:'development',devtool:'source-map',entry:resolve('../src/index.js'),output:{path:resolve('../dist'),} };
|
|
41
|
+
</code></pre>
|
|
42
|
+
<p>源文件 <code>src/index.js</code>:</p>
|
|
43
|
+
<pre><code>document.writeln('Hello webpack!');
|
|
44
|
+
</code></pre>
|
|
45
|
+
<p>现在我们运行命令 <code>webpack --config build/webpack.config.js</code> ,打包完成后会多出一个输出目录 dist:</p>
|
|
46
|
+
<ul>
|
|
47
|
+
<li><p>build</p>
|
|
48
|
+
</li>
|
|
49
|
+
<li><p>webpack.config.js</p>
|
|
50
|
+
</li>
|
|
51
|
+
<li><p>dist</p>
|
|
52
|
+
</li>
|
|
53
|
+
<li><p>main.js</p>
|
|
54
|
+
</li>
|
|
55
|
+
<li><p>src</p>
|
|
56
|
+
</li>
|
|
57
|
+
<li><p>index.js</p>
|
|
58
|
+
</li>
|
|
59
|
+
<li><p>package.json</p>
|
|
60
|
+
</li>
|
|
61
|
+
</ul>
|
|
62
|
+
<p><code>main</code> 是 webpack 默认设置的输出文件名,我们快速瞄一眼这个文件:</p>
|
|
63
|
+
<p><code>dist/main.js</code>:</p>
|
|
64
|
+
<pre><code>(function(modules){// ...
|
|
65
|
+
})({"./src/index.js":(function(){// ...
|
|
66
|
+
})});
|
|
67
|
+
</code></pre>
|
|
68
|
+
<p>整个文件只含一个立即执行函数(IIFE),我们称它为 <code>webpackBootstrap</code>,它仅接收一个对象 —— 未加载的 模块集合(modules),这个 modules 对象的 key 是一个路径,value 是一个函数。你也许会问,这里的模块是什么?它们又是如何加载的呢?</p>
|
|
69
|
+
<h2 id="模块"><a href="#模块" class="headerlink" title="模块"></a>模块</h2><p>别着急,在细看产出代码前,我们先丰富一下源代码:</p>
|
|
70
|
+
<p>项目目录:</p>
|
|
71
|
+
<ul>
|
|
72
|
+
<li><p>build</p>
|
|
73
|
+
</li>
|
|
74
|
+
<li><p>webpack.config.js</p>
|
|
75
|
+
</li>
|
|
76
|
+
<li><p>src</p>
|
|
77
|
+
</li>
|
|
78
|
+
<li><p>utils</p>
|
|
79
|
+
</li>
|
|
80
|
+
<li><p>math.js</p>
|
|
81
|
+
</li>
|
|
82
|
+
<li><p>index.js</p>
|
|
83
|
+
</li>
|
|
84
|
+
<li><p>package.json</p>
|
|
85
|
+
</li>
|
|
86
|
+
</ul>
|
|
87
|
+
<p>新文件 <code>src/utils/math.js</code>:</p>
|
|
88
|
+
<pre><code>exportconstplus=(a,b)=>{returna+b;};exportconstminus=(a,b)=>{returna-b;};
|
|
89
|
+
</code></pre>
|
|
90
|
+
<p><code>src/index.js</code>:</p>
|
|
91
|
+
<pre><code>import{plus,minus}from'./utils/math.js';document.writeln('Hello webpack!');document.writeln('1 + 2: ',plus(1,2));document.writeln('1 - 2: ',minus(1,2));
|
|
92
|
+
</code></pre>
|
|
93
|
+
<p>我们按照 ES 规范的模块化语法写了一个简单的模块 <code>src/utils/math.js</code>,给 <code>src/index.js</code> 引用。目前,虽然各大浏览器开始支持通过 <code><script type="module"></code> 的方式支持 ES6 Module,但<a href="https://link.zhihu.com/?target=https://caniuse.com/%23feat=es6-module">还需时间覆盖</a>。Webpack 用自己的方式支持了 ES6 Module 规范,前面提到的 module 就是和 ES6 module 对应的概念。</p>
|
|
94
|
+
<p>接下来我们看一下这些模块是如何通 ES5 代码实现的。再次运行命令 <code>webpack --config build/webpack.config.js</code> 后查看输出文件:</p>
|
|
95
|
+
<p><code>dist/main.js</code>:</p>
|
|
96
|
+
<pre><code>(function(modules){// ...
|
|
97
|
+
})({"./src/index.js":(function(){// ...
|
|
98
|
+
}),"./src/utils/math.js":(function(){// ...
|
|
99
|
+
})});
|
|
100
|
+
</code></pre>
|
|
101
|
+
<p>IIFE 传入的 modules 对象里多了一个键值对,对应着新模块 <code>src/utils/math.js</code>,这和我们在源代码中拆分的模块互相呼应。然而,有了 modules 只是第一步,这份文件最终达到的效果应该是让各个模块按开发者编排的顺序运行。</p>
|
|
102
|
+
<h2 id="探究-webpackBootstrap"><a href="#探究-webpackBootstrap" class="headerlink" title="探究 webpackBootstrap"></a>探究 webpackBootstrap</h2><p>接下来看看 <code>webpackBootstrap</code> 函数中有些什么:</p>
|
|
103
|
+
<pre><code>// webpackBootstrap
|
|
104
|
+
(function(modules){// 缓存 __webpack_require__ 函数加载过的模块
|
|
105
|
+
varinstalledModules={};/**
|
|
106
|
+
* Webpack 加载函数,用来加载 webpack 定义的模块
|
|
107
|
+
* @param {String} moduleId 模块 ID,一般为模块的源码路径,如 "./src/index.js"
|
|
108
|
+
* @returns {Object} exports 导出对象
|
|
109
|
+
*/function__webpack_require__(moduleId){// ...
|
|
110
|
+
}// 在 __webpack_require__ 函数对象上挂载一些变量及函数 ...
|
|
111
|
+
// 传入表达式的值为 "./src/index.js"
|
|
112
|
+
return__webpack_require__(__webpack_require__.s="./src/index.js");})(/* modules */);
|
|
113
|
+
</code></pre>
|
|
114
|
+
<p>可以看到其实主要做了两件事:</p>
|
|
115
|
+
<ol>
|
|
116
|
+
<li>定义一个模块加载函数 <code>__webpack_require__</code>。</li>
|
|
117
|
+
<li>使用加载函数加载入口模块 <code>"./src/index.js"</code>。</li>
|
|
118
|
+
</ol>
|
|
119
|
+
<p>整个 <code>webpackBootstrap</code> 中只出现了入口模块的影子,那其他模块又是如何加载的呢?我们顺着 <code>__webpack_require__("./src/index.js")</code> 细看加载函数的内部逻辑:</p>
|
|
120
|
+
<pre><code>// ...
|
|
121
|
+
function__webpack_require__(moduleId){// 重复加载则利用缓存
|
|
122
|
+
if(installedModules[moduleId]){returninstalledModules[moduleId].exports;}// 如果是第一次加载,则初始化模块对象,并缓存
|
|
123
|
+
varmodule=installedModules[moduleId]={i:moduleId,// 模块 ID
|
|
124
|
+
l:false,// 模块加载标识
|
|
125
|
+
exports:{}// 模块导出对象
|
|
126
|
+
};/**
|
|
127
|
+
* 执行模块
|
|
128
|
+
* @param module.exports -- 模块导出对象引用,改变模块包裹函数内部的 this 指向
|
|
129
|
+
* @param module -- 当前模块对象引用
|
|
130
|
+
* @param module.exports -- 模块导出对象引用
|
|
131
|
+
* @param __webpack_require__ -- 用于在模块中加载其他模块
|
|
132
|
+
*/modules[moduleId].call(module.exports,module,module.exports,__webpack_require__);// 模块加载标识置为已加载
|
|
133
|
+
module.l=true;// 返回当前模块的导出对象引用
|
|
134
|
+
returnmodule.exports;}// ...
|
|
135
|
+
</code></pre>
|
|
136
|
+
<p>首先,加载函数使用了闭包变量 <code>installedModules</code>,用来将已加载过的模块保存在内存中。 接着是初始化模块对象,并把它挂载到缓存里。然后是模块的执行过程,加载入口文件时 <code>modules[moduleId]</code> 其实就是 <code>./src/index.js</code> 对应的模块函数。执行模块函数前传入了跟模块相关的几个实参,让模块可以导出内容,以及加载其他模块的导出。最后标识该模块加载完成,返回模块的导出内容。</p>
|
|
137
|
+
<p>根据 <code>__webpack_require__</code> 的缓存和导出逻辑,我们得知在整个 IIFE 运行过程中,加载已缓存的模块时,都会直接返回 <code>installedModules[moduleId].exports</code>,换句话说,相同的模块只有在第一次引用的时候才会执行模块本身。</p>
|
|
138
|
+
<h2 id="模块执行函数"><a href="#模块执行函数" class="headerlink" title="模块执行函数"></a>模块执行函数</h2><p><code>__webpack_require__</code> 中通过 <code>modules[moduleId].call()</code> 运行了模块执行函数,下面我们就进入到 <code>webpackBootstrap</code> 的参数部分,看看模块的执行函数。</p>
|
|
139
|
+
<pre><code>// webpackBootstrap
|
|
140
|
+
(function(modules){// ...
|
|
141
|
+
})({/*** 入口模块 ./src/index.js ***/"./src/index.js":(function(module,__webpack_exports__,__webpack_require__){"use strict";// 用于区分 ES 模块和其他模块规范,不影响理解 demo,战略跳过。
|
|
142
|
+
__webpack_require__.r(__webpack_exports__);// 源模块代码中,`import {plus, minus} from './utils/math.js';` 语句被 loader 解析转化。
|
|
143
|
+
// 加载 "./src/utils/math.js" 模块,
|
|
144
|
+
/* harmony import */var_utils_math_js__WEBPACK_IMPORTED_MODULE_0__=__webpack_require__(/*! ./utils/math.js */"./src/utils/math.js");document.writeln('Hello webpack!');document.writeln('1 + 2: ',Object(_utils_math_js__WEBPACK_IMPORTED_MODULE_0__["plus"])(1,2));document.writeln('1 - 2: ',Object(_utils_math_js__WEBPACK_IMPORTED_MODULE_0__["minus"])(1,2));}),/*** 工具模块 ./src/utils/math.js ***/"./src/utils/math.js":(function(module,__webpack_exports__,__webpack_require__){"use strict";// 同 "./src/index.js"
|
|
145
|
+
__webpack_require__.r(__webpack_exports__);// 源模块代码中,`export` 语句被 loader 解析转化。
|
|
146
|
+
// 导出 __webpack_exports__
|
|
147
|
+
/* harmony export (binding) */__webpack_require__.d(__webpack_exports__,"plus",function(){returnplus;});/* harmony export (binding) */__webpack_require__.d(__webpack_exports__,"minus",function(){returnminus;});constplus=(a,b)=>{returna+b;};constminus=(a,b)=>{returna-b;};})});
|
|
148
|
+
</code></pre>
|
|
149
|
+
<p>执行顺序是:入口模块 -> 工具模块 -> 入口模块。入口模块中首先就通过 <code>__webpack_require__("./src/utils/math.js")</code> 拿到了工具模块的 <code>exports</code> 对象。再看工具模块,ES 导出语法转化成了<code>__webpack_require__.d(__webpack_exports__, [key], [getter])</code>,而 <code>__webpack_require__.d</code> 函数的定义在 <code>webpackBootstrap</code> 内:</p>
|
|
150
|
+
<pre><code>// ...
|
|
151
|
+
// 定义 exports 对象导出的属性。
|
|
152
|
+
__webpack_require__.d=function(exports,name,getter){// 如果 exports (不含原型链上)没有 [name] 属性,定义该属性的 getter。
|
|
153
|
+
if(!__webpack_require__.o(exports,name)){Object.defineProperty(exports,name,{enumerable:true,get:getter});} };// 包装 Object.prototype.hasOwnProperty 函数。
|
|
154
|
+
__webpack_require__.o=function(object,property){returnObject.prototype.hasOwnProperty.call(object,property);};// ...
|
|
155
|
+
</code></pre>
|
|
156
|
+
<p>可见 <code>__webpack_require__.d</code> 其实就是 <code>Object.defineProperty</code> 的简单包装(怪不得叫 d 呢)。</p>
|
|
157
|
+
<p>回顾一下,<code>__webpack_exports__</code> 原本在 <code>__webpack_require__</code> 中创建,初始值为 <code>{}</code>。这个导出对象一路传到工具模块 <code>math.js</code> 中,被添加上 <code>plus</code> 和 <code>minus</code>,然后又在 <code>__webpack_require__</code> 函数最后导出,为入口模块 <code>index.js</code> 的执行函数所用。</p>
|
|
158
|
+
<p>exports 的一生:<br><img src="https://pic3.zhimg.com/v2-6ea7c7bee5872c9edc6431273a5d830e_b.jpg" alt="xuehuayu.cn"><img src="https://pic3.zhimg.com/80/v2-6ea7c7bee5872c9edc6431273a5d830e_hd.jpg" alt="xuehuayu.cn"><br>引用工具模块导出的变量后,入口模块再执行它剩余的部分。至此,Webpack 基本的模块执行过程就结束了。</p>
|
|
159
|
+
<p>以上内容可<a href="https://link.zhihu.com/?target=https://github.com/pobusama/how-webpack-works">克隆示例代码库调试</a>,分支为 <code>demo1</code>。</p>
|
|
160
|
+
<p>除了 ES6 Module 规范,Webpack 同样支持 CommonJS 与 AMD 规范,你可以替换模块化规范,重新打包来观察它们的区别。</p>
|
|
161
|
+
<h2 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h2><p>好了,我们用流程图总结一下 Webpack 模块的加载思路:</p>
|
|
162
|
+
<p><img src="https://pic4.zhimg.com/v2-dbd5e17901ad814d0757ad67ad58047f_b.jpg" alt="xuehuayu.cn"><img src="https://pic4.zhimg.com/80/v2-dbd5e17901ad814d0757ad67ad58047f_hd.jpg" alt="xuehuayu.cn"></p>
|
|
163
|
+
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><p><a href="https://link.zhihu.com/?target=https://webpack.js.org/glossary/%23m">Glossary&#8203;webpack.js.org</a></p>
|
|
164
|
+
</div><iframe src="/donate/?AliPayQR=https://store.xuehuayu.cn/store/public/alipay_qr.jpg&WeChatQR=https://store.xuehuayu.cn/store/public/wechat_qr.png&UnionPayQR=null&GitHub=null&BTCQR=null&BTCKEY=null&PayPal=https://www.paypal.com/paypalme/OrdinaryPerson" style="overflow-x:hidden; overflow-y:hidden; border:0xp none #fff; min-height:240px; width:100%;" frameborder="0" scrolling="no"></iframe><div><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者:</strong>雪花雨</li><li class="post-copyright-link"><strong>本文链接:</strong><a href="/article/38530.html">https://xuehuayu.cn/article/38530.html</a></li><li class="post-copyright-license"><strong>版权声明:</strong>① 标为原创的文章为博主原创,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接。② 转载文章来自网络,已标明出处,<a href="mailto:cainiaoblog@126.com">侵删</a>。</li></ul></div><br><div class="tags"><a href="/tags/%E8%BD%AC%E8%BD%BD/">转载</a></div><div class="post-nav"><a class="pre" href="/article/22565.html">使用 Hooks 优化 React 组件</a><a class="next" href="/article/64051.html">48个 JS 开发常用工具函数</a></div><div id="vcomment"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@waline/client@v2/dist/waline.min.css"><script src="//cdn.jsdelivr.net/npm/@waline/client@v2/dist/waline.js"></script><script>const origin = window.location.origin
|
|
165
|
+
const serverURL = origin.includes('cainiaoblog') ? 'https://cmts.cainiaoblog.cn' : 'https://cmts.xuehuayu.cn'
|
|
166
|
+
const locale = {
|
|
167
|
+
placeholder: '请正确填写昵称和邮箱,方便接收回复通知~',
|
|
168
|
+
sofa: '沙发空缺中,还不快抢~',
|
|
169
|
+
};
|
|
170
|
+
Waline.init({
|
|
171
|
+
el: '#vcomment',
|
|
172
|
+
serverURL: serverURL,
|
|
173
|
+
pageSize: '20',
|
|
174
|
+
visitor: true, // 阅读量统计
|
|
175
|
+
requiredMeta: ['nick', 'mail'],
|
|
176
|
+
emoji: [
|
|
177
|
+
'https://cdn.jsdelivr.net/gh/walinejs/emojis@1.0.0/weibo',
|
|
178
|
+
'https://cdn.jsdelivr.net/gh/walinejs/emojis@1.0.0/qq',
|
|
179
|
+
'https://cdn.jsdelivr.net/gh/walinejs/emojis@1.0.0/tw-emoji',
|
|
180
|
+
],
|
|
181
|
+
locale,
|
|
182
|
+
pageview: true
|
|
183
|
+
})
|
|
184
|
+
</script></div></div></div><div class="pure-u-1 pure-u-md-1-4 fixed-search hidden_mid_and_down"><div id="sidebar"><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/search.min.js"></script><div class="widget"><div class="widget-search"><input class="search" type="radio" name="search" value="baidu" id="baidu" checked="checked"/><label class="label" for="baidu" title="百度全站搜索">百度</label><input class="search" type="radio" name="search" value="google" id="google"/><label class="label" for="google" title="谷歌全站搜索">谷歌</label><input class="search" type="radio" name="search" value="self" id="self"/><label class="label" for="self" title="使用站内搜索">站内</label></div><div class="widget" id="search"><form class="search-form" action="//www.baidu.com/baidu" method="get" accept-charset="utf-8" target="_blank"><input type="search" name="word" maxlength="30" placeholder="百度全站搜索"/><input type="hidden" name="si" value="xuehuayu.cn"/><input type="hidden" name="cl" value="3"/><input type="hidden" name="ct" value="2097152"/><input type="hidden" name="s" value="on"/><input class="search-submit" type="submit" value=""/></form></div></div><script>$('input[type=radio][name=search]').change(function() {
|
|
185
|
+
var val = $(this).val()
|
|
186
|
+
var self = '<div class="search-form"><input id="local-search-input" placeholder="站内搜索,首次慢" type="search" name="q" results="0"><input class="search-submit" type="submit" value=""/><div id="local-search-result"></div></div>'
|
|
187
|
+
var google = '<form class="search-form" action="//www.google.com/search" method="get" accept-charset="utf-8" target="_blank"><input type="search" name="q" maxlength="30" placeholder="谷歌全站搜索"><input type="hidden" name="sitesearch" value="xuehuayu.cn"><input class="search-submit" type="submit" value=""/></form>'
|
|
188
|
+
var baidu = '<form class="search-form" action="//www.baidu.com/baidu" method="get" accept-charset="utf-8" target="_blank"><input type="search" name="word" maxlength="30" placeholder="百度全站搜索"><input type="hidden" name="si" value="xuehuayu.cn"><input type="hidden" name="cl" value="3"><input type="hidden" name="ct" value="2097152"><input type="hidden" name="s" value="on"><input class="search-submit" type="submit" value=""/></form>'
|
|
189
|
+
|
|
190
|
+
if (val === 'self') {
|
|
191
|
+
$('#search').html(self)
|
|
192
|
+
var search_path = 'search.xml';
|
|
193
|
+
if (search_path.length == 0) {
|
|
194
|
+
search_path = 'search.xml';
|
|
195
|
+
}
|
|
196
|
+
var path = 'https://cdn.staticaly.com/gh/npljy/npljy.github.io/main/' + search_path;
|
|
197
|
+
searchFunc(path, 'local-search-input', 'local-search-result');
|
|
198
|
+
} else if (val === 'baidu') {
|
|
199
|
+
$('#search').html(baidu)
|
|
200
|
+
} else if (val === 'google') {
|
|
201
|
+
$('#search').html(google)
|
|
202
|
+
}
|
|
203
|
+
})</script><div class="widget widget-wxmp"><img alt="微信公众号" width="100%" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/mp-mini.jpg"/></div><div class="widget widget-categories"><div class="widget-title"><i class="fa fa-folder-o"> 分类</i></div><ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/CDN/">CDN</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/IDE/">IDE</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/git/">git</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/hexo/">hexo</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%85%AC%E5%85%B1%E8%8A%82%E6%97%A5/">公共节日</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%88%86%E4%BA%AB/">分享</a><span class="category-list-count">29</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%89%8D%E7%AB%AF/">前端</a><span class="category-list-count">191</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%89%8D%E7%AB%AF/%E6%B5%8F%E8%A7%88%E5%99%A8/">浏览器</a><span class="category-list-count">1</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%89%8D%E7%AB%AF/%E6%B5%8F%E8%A7%88%E5%99%A8/%E7%BD%91%E7%BB%9C%E5%8D%8F%E8%AE%AE/">网络协议</a><span class="category-list-count">1</span></li></ul></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%8D%9A%E5%AE%A2/">博客</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%B7%A5%E5%85%B7/">工具</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%B9%BF%E5%91%8A%E8%BF%87%E6%BB%A4/">广告过滤</a><span class="category-list-count">8</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%B9%BF%E5%91%8A%E8%BF%87%E6%BB%A4/%E8%A7%86%E9%A2%91%E7%94%B5%E8%A7%86%E5%B0%8F%E8%AF%B4%E6%BC%AB%E7%94%BB%E8%B5%84%E8%AE%AF%E9%9F%B3%E4%B9%90/">视频电视小说漫画资讯音乐</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%AD%A3%E5%88%99/">正则</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%B5%8F%E8%A7%88%E5%99%A8/">浏览器</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%AE%97%E6%B3%95/">算法</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%B5%84%E8%AE%AF/">资讯</a><span class="category-list-count">16</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%BD%BB%E6%9D%BE%E4%B8%80%E5%88%BB/">轻松一刻</a><span class="category-list-count">1</span></li></ul></div><div class="widget widget-tags"><div class="widget-title"><i class="fa fa-star-o"> 标签</i></div><div class="tagcloud"><a href="/tags/%E5%8E%9F%E5%88%9B/" style="font-size: 15px;">原创</a> <a href="/tags/%E8%BD%AC%E8%BD%BD/" style="font-size: 15px;">转载</a> <a href="/tags/%E6%89%8B%E5%86%99/" style="font-size: 15px;">手写</a> <a href="/tags/%E7%96%AB%E6%83%85/" style="font-size: 15px;">疫情</a> <a href="/tags/%E5%A8%B1%E4%B9%90/" style="font-size: 15px;">娱乐</a></div></div><div class="widget widget-recent-posts"><div class="widget-title"><i class="fa fa-file-o"> 最近文章</i></div><ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/article/c5576490.html">面试准备</a></li><li class="post-list-item"><a class="post-list-link" href="/article/84c9ccd6.html">精读《SolidJS》</a></li><li class="post-list-item"><a class="post-list-link" href="/article/53803e6.html">git提交注释规范</a></li><li class="post-list-item"><a class="post-list-link" href="/article/a4145266.html">前端数组拍平flat一行代码</a></li><li class="post-list-item"><a class="post-list-link" href="/article/bedea419.html">一道前端this面试题</a></li><li class="post-list-item"><a class="post-list-link" href="/article/bf7e7421.html">git多账号配置</a></li></ul></div><div class="widget widget-links"><div class="widget-title"><i class="fa fa-external-link"> 友情链接</i></div><ul></ul><a href="https://github.com/npljy?utm_source=xuehuayu.cn" title="GitHub" target="_blank">GitHub</a><ul></ul><a href="https://laonongmin.online?utm_source=xuehuayu.cn" title="看看" target="_blank">看看</a><ul></ul><a href="https://cainiaoblog.cn?utm_source=xuehuayu.cn" title="菜鸟博客" target="_blank">菜鸟博客</a><ul></ul><a href="mailto:cainiaoblog@126.com" title="友链联系" target="_blank">友链联系</a></div></div></div><div class="pure-u-1 pure-u-md-3-4"><div id="footer"><div class="flex-block justify-center align-center flex-wrap"><a class="gxba-link" id="gxba" rel="nofollow" target="_blank" href="http://beian.miit.gov.cn/">京ICP备20007647号-2</a><a class="gaba-link" id="gaba-link" rel="nofollow" target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=11010802031264"><img class="nofancybox" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/gaba.png" alt=""/><span id="gaba">京公网安备 11010802031264号</span><span style="padding-right: 10px;"></span></a><span>Copyright © 2023 </span><a href="/." rel="nofollow">前端壹菜鸟. </a><script>(function(){
|
|
204
|
+
var cnb =window.location.origin.indexOf('cainiaoblog') !==-1
|
|
205
|
+
if (cnb) {
|
|
206
|
+
var gxba =document.getElementById('gxba')
|
|
207
|
+
var gaba =document.getElementById('gaba')
|
|
208
|
+
var gabaLink =document.getElementById('gaba-link')
|
|
209
|
+
gxba.innerText ='京ICP备20007647号-1'
|
|
210
|
+
gaba.innerText ='京公网安备 11010802031254号'
|
|
211
|
+
gabaLink.setAttribute('href','http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=11010802031254')
|
|
212
|
+
}
|
|
213
|
+
})()</script></div><div class="flex-block justify-center align-center flex-wrap"><a rel="nofollow" target="_blank" href="https://v6.51.la/s/Guz3lwHCsVme9Cu"><div class="busuanzi_container" id="busuanzi_container"><span class="busuanzi_container_site_pv" id="busuanzi_container_site_pv">访问量:<span id="busuanzi_value_site_pv"><i class="fa fa-spinner"></i></span></span><span class="busuanzi_container_site_uv" id="busuanzi_container_site_uv">访客数:<span id="busuanzi_value_site_uv"><i class="fa fa-spinner"></i></span></span><span style="padding-right: 10px;"></span></div></a><a rel="nofollow" target="_blank" href="https://www.upyun.com/?utm_source=lianmeng&utm_medium=referral"><span style="font-weight:bold;letter-spacing: 1px;">本网站由 </span><img class="nofancybox" height="32" style="vertical-align: middle" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/upy_logo.min.svg" alt=""/><span style="font-weight:bold;letter-spacing: 1px;">提供CDN加速/云存储服务</span></a><span style="padding-right: 10px;"></span><a rel="nofollow" style="font-size:18px;font-weight:bold;vertical-align: middle" target="_blank" title="注册就送代金券可直接使用" href="https://console.upyun.com/register/?invite=HyDsjZHIL">注册</a></div></div></div></div><a class="show" id="rocket" href="#top"></a><div class="darkmode-toggle">🌓</div><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/totop.min.js" async></script><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/dark.min.js" async></script><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/codeblock-resizer.min.js"></script><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/smartresize.min.js"></script></div></body></html>
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
<!DOCTYPE html><html lang="zh-CN" data-dark><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"><meta content="yes" name="apple-mobile-web-app-capable"><meta content="black-translucent" name="apple-mobile-web-app-status-bar-style"><meta content="telephone=no" name="format-detection"><meta name="description" content="前端壹菜鸟,记录工作难题,关注前端知识,收集精彩博文,做技术的搬运工"><meta name="msvalidate.01" content="0FE4D8B3381D3D87088996B886E1E2BD"><title>高频面试题深入解析/ 周刊 05 | 前端壹菜鸟</title><link rel="stylesheet" type="text/css" href="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/css/style.min.css"><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/normalize.css@latest/normalize.min.css"><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/purecss@latest/build/pure-min.min.css"><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/purecss@latest/build/grids-responsive-min.css"><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css"><link rel="icon" mask="" sizes="any" href="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/blog.ico"><link rel="Shortcut Icon" type="image/x-icon" href="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/blog.ico"><link rel="apple-touch-icon" href="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/blog.png"><link rel="apple-touch-icon-precomposed" href="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/blog.png"><link rel="alternate" type="application/atom+xml" href="/atom.xml"><script type="text/javascript" src="//cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8385136408348258"></script><script src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script><script>(function(){
|
|
2
|
+
var bp = document.createElement('script');
|
|
3
|
+
var curProtocol = window.location.protocol.split(':')[0];
|
|
4
|
+
if (curProtocol === 'https'){
|
|
5
|
+
bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
|
|
6
|
+
}
|
|
7
|
+
else{
|
|
8
|
+
bp.src = 'http://push.zhanzhang.baidu.com/push.js';
|
|
9
|
+
}
|
|
10
|
+
var s = document.getElementsByTagName("script")[0];
|
|
11
|
+
s.parentNode.insertBefore(bp, s);
|
|
12
|
+
})();
|
|
13
|
+
</script><script>(function(){
|
|
14
|
+
var src = 'https://jspassport.ssl.qhimg.com/11.0.1.js?d182b3f28525f2db83acfaaf6e696dba';
|
|
15
|
+
document.write('<script src="' + src + '" id="sozz"><\/script>');
|
|
16
|
+
})();
|
|
17
|
+
</script><script>var _hmt=_hmt||[];(function(){var hm=document.createElement("script");hm.src='https://hm.baidu.com/hm.js?011f0b44e8452bfa57fbfa23c5fe7683';var s=document.getElementsByTagName("script")[0];s.parentNode.insertBefore(hm, s);})();
|
|
18
|
+
</script><script async src="https://www.googletagmanager.com/gtag/js?id=UA-150860401-1"></script><script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);};gtag('js',new Date());gtag('config','UA-150860401-1');
|
|
19
|
+
</script><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/fixedPage.min.js"></script><meta name="generator" content="Hexo 6.3.0"></head><body><div class="body_container"><div id="header"><div class="site-name"><a id="logo" href="/.">前端壹菜鸟</a><p class="description">关注前端知识,收集精彩博文,做技术的搬运工</p></div><div id="nav-menu"><a class="current" href="/." target="_self"><i class="fa fa-home"> 首页</i></a><a href="/archives/" target="_self"><i class="fa fa-archive"> 归档</i></a><a href="/guestbook/" target="_self"><i class="fa fa-comments"> 留言</i></a><a href="/about/" target="_self"><i class="fa fa-user"> 关于</i></a><a href="https://laonongmin.online/" target="_target"><i class="fa fa-group"> 看看</i></a><a href="https://love.xuehuayu.cn/" target="_target"><i class="fa fa-heart"> LOVE</i></a></div></div><div class="pure-g" id="layout"><div class="pure-u-1 pure-u-md-3-4"><div class="content_container"><div class="post"><h1 class="post-title c-post">高频面试题深入解析/ 周刊 05</h1><div class="post-meta"><span class="date">2019-10-03</span><span> | </span><span class="tag"><a href="/tags/%E8%BD%AC%E8%BD%BD/">转载 </a></span><span> | </span><span class="category"><a href="/categories/%E5%88%86%E4%BA%AB/">分享 </a></span><span id="busuanzi_container_page_pv"> | <span id="busuanzi_value_page_pv"><i class="fa fa-spinner"></i></span><span> 阅读</span></span><span class="post-time"><span class="post-meta-item-text"> | </span><span class="post-meta-item-icon"><i class="fa fa-keyboard-o"></i><span class="post-count"> 1.8k</span><span class="post-meta-item-text"> 字</span></span></span><span class="post-time"> | <span class="post-meta-item-icon"><i class="fa fa-hourglass-half"></i><span class="post-count"> 7</span><span class="post-meta-item-text"> 分钟</span></span></span></div><a class="disqus-comment-count" href="/article/38743.html#vcomment"><span class="waline-comment-count" data-path="/article/38743.html"></span><span> 条评论</span></a><div class="post-content"><p><code>原文地址:https://github.com/YvetteLau/Blog/issues/36</code></p>
|
|
20
|
+
<span id="more"></span>
|
|
21
|
+
|
|
22
|
+
<h3 id="关于【Step-By-Step】"><a href="#关于【Step-By-Step】" class="headerlink" title="关于【Step-By-Step】"></a>关于【Step-By-Step】</h3><blockquote>
|
|
23
|
+
<p><a target="_blank" rel="noopener" href="https://github.com/YvetteLau/Step-By-Step">Step-By-Step</a> (点击进入项目) 是我于 <code>2019-05-20</code> 开始的一个项目,每个工作日发布一道面试题。<br>每个周末我会仔细阅读大家的答案,整理最一份较优答案出来,因本人水平有限,有误的地方,大家及时指正。</p>
|
|
24
|
+
</blockquote>
|
|
25
|
+
<hr>
|
|
26
|
+
<blockquote>
|
|
27
|
+
<p>本周面试题一览:</p>
|
|
28
|
+
</blockquote>
|
|
29
|
+
<ul>
|
|
30
|
+
<li>实现 Promise.race 方法</li>
|
|
31
|
+
<li>JSONP 原理及简单实现</li>
|
|
32
|
+
<li>实现一个数组去重的方法</li>
|
|
33
|
+
<li>清楚浮动的方法有哪些</li>
|
|
34
|
+
<li>编写一个通用的柯里化函数 currying</li>
|
|
35
|
+
</ul>
|
|
36
|
+
<h3 id="20-实现-Promise-race-方法"><a href="#20-实现-Promise-race-方法" class="headerlink" title="20. 实现 Promise.race 方法"></a>20. 实现 Promise.race 方法</h3><p>在实现 <code>Promise.race</code> 方法之前,我们首先要知道 <code>Promise.race</code> 的功能和特点,因为在清楚了 <code>Promise.race</code> 功能和特点的情况下,我们才能进一步去写实现。</p>
|
|
37
|
+
<h4 id="Promise-race-功能"><a href="#Promise-race-功能" class="headerlink" title="Promise.race 功能"></a>Promise.race 功能</h4><p><code>Promise.race(iterable)</code> 返回一个 promise,一旦 <code>iterable</code> 中的一个 <code>promise</code> 状态是 <code>fulfilled</code> / <code>rejected</code> ,那么 <code>Promise.race</code> 返回的 <code>promise</code> 状态是 <code>fulfilled</code> / <code>rejected</code>.</p>
|
|
38
|
+
<pre><code>let p =Promise.race([p1, p2, p3]);
|
|
39
|
+
</code></pre>
|
|
40
|
+
<p>只要p1、p2、p3之中有一个实例率先改变状态,p的状态就跟着改变。那个率先改变的 <code>Promise</code> 实例的返回值,就传递给 p 的回调函数。</p>
|
|
41
|
+
<h4 id="Promise-race-的特点"><a href="#Promise-race-的特点" class="headerlink" title="Promise.race 的特点"></a>Promise.race 的特点</h4><blockquote>
|
|
42
|
+
<p>Promise.race 的返回值是一个 promise 实例</p>
|
|
43
|
+
</blockquote>
|
|
44
|
+
<ul>
|
|
45
|
+
<li>如果传入的参数为空的可迭代对象,那么 <code>Promise.race</code> 返回的 <code>promise</code> 永远是 <code>pending</code> 态</li>
|
|
46
|
+
<li>如果传入的参数中不包含任何 <code>promise</code>,<code>Promise.race</code> 会返回一个处理中(pending)的 <code>promise</code></li>
|
|
47
|
+
<li>如果 <code>iterable</code> 包含一个或多个非 <code>promise</code> 值或已经解决的promise,则 <code>Promise.race</code> 将解析为 <code>iterable</code> 中找到的第一个值。</li>
|
|
48
|
+
</ul>
|
|
49
|
+
<h4 id="Promise-race-的实现"><a href="#Promise-race-的实现" class="headerlink" title="Promise.race 的实现"></a>Promise.race 的实现</h4><pre><code>Promise.race=function (promises) {
|
|
50
|
+
//promises传入的是可迭代对象(省略参数合法性判断)
|
|
51
|
+
promises =Array.from(promises);//将可迭代对象转换为数组returnnewPromise((resolve, reject) => {
|
|
52
|
+
if (promises.length===0) {
|
|
53
|
+
//空的可迭代对象;//用于在pending态
|
|
54
|
+
} else {
|
|
55
|
+
for (let i =0; i <promises.length; i++) {
|
|
56
|
+
Promise.resolve(promises[i]).then((data) => {
|
|
57
|
+
resolve(data);
|
|
58
|
+
}).catch((reason) => {
|
|
59
|
+
reject(reason);
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
</code></pre>
|
|
66
|
+
<h3 id="21-JSONP原理及简单实现"><a href="#21-JSONP原理及简单实现" class="headerlink" title="21. JSONP原理及简单实现"></a>21. JSONP原理及简单实现</h3><p>尽管浏览器有同源策略,但是 <code><script></code> 标签的 <code>src</code> 属性不会被同源策略所约束,可以获取任意服务器上的脚本并执行。<code>jsonp</code> 通过插入 <code>script</code> 标签的方式来实现跨域,参数只能通过 <code>url</code> 传入,仅能支持 <code>get</code> 请求。</p>
|
|
67
|
+
<h4 id="实现原理"><a href="#实现原理" class="headerlink" title="实现原理:"></a>实现原理:</h4><ul>
|
|
68
|
+
<li>Step1: 创建 callback 方法</li>
|
|
69
|
+
<li>Step2: 插入 script 标签</li>
|
|
70
|
+
<li>Step3: 后台接受到请求,解析前端传过去的 callback 方法,返回该方法的调用,并且数据作为参数传入该方法</li>
|
|
71
|
+
<li>Step4: 前端执行服务端返回的方法调用</li>
|
|
72
|
+
</ul>
|
|
73
|
+
<h4 id="jsonp源码实现"><a href="#jsonp源码实现" class="headerlink" title="jsonp源码实现"></a>jsonp源码实现</h4><pre><code>functionjsonp({url, params, callback}) {
|
|
74
|
+
returnnewPromise((resolve, reject) => {
|
|
75
|
+
//创建script标签let script =document.createElement('script');
|
|
76
|
+
//将回调函数挂在 window 上window[callback] =function(data) {
|
|
77
|
+
resolve(data);
|
|
78
|
+
//代码执行后,删除插入的script标签document.body.removeChild(script);
|
|
79
|
+
}
|
|
80
|
+
//回调函数加在请求地址上
|
|
81
|
+
params = {...params, callback} //wb=b&callback=showlet arrs = [];
|
|
82
|
+
for(let key in params) {
|
|
83
|
+
arrs.push(`${key}=${params[key]}`);
|
|
84
|
+
}
|
|
85
|
+
script.src=`${url}?${arrs.join('&')}`;
|
|
86
|
+
document.body.appendChild(script);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
</code></pre>
|
|
90
|
+
<p>使用:</p>
|
|
91
|
+
<pre><code>functionshow(data) {
|
|
92
|
+
console.log(data);
|
|
93
|
+
}
|
|
94
|
+
jsonp({
|
|
95
|
+
url:'http://localhost:3000/show',
|
|
96
|
+
params: {
|
|
97
|
+
//code
|
|
98
|
+
},
|
|
99
|
+
callback:'show'
|
|
100
|
+
}).then(data=> {
|
|
101
|
+
console.log(data);
|
|
102
|
+
});
|
|
103
|
+
</code></pre>
|
|
104
|
+
<p>服务端代码(node):</p>
|
|
105
|
+
<pre><code>//express启动一个后台服务let express =require('express');
|
|
106
|
+
let app =express();
|
|
107
|
+
|
|
108
|
+
app.get('/show', (req, res) => {
|
|
109
|
+
let {callback} =req.query; //获取传来的callback函数名,callback是keyres.send(`${callback}('Hello!')`);
|
|
110
|
+
});
|
|
111
|
+
app.listen(3000);
|
|
112
|
+
</code></pre>
|
|
113
|
+
<h3 id="22-实现一个数组去重的方法"><a href="#22-实现一个数组去重的方法" class="headerlink" title="22. 实现一个数组去重的方法"></a>22. 实现一个数组去重的方法</h3><h4 id="法1-利用ES6新增数据类型-Set"><a href="#法1-利用ES6新增数据类型-Set" class="headerlink" title="法1: 利用ES6新增数据类型 Set"></a>法1: 利用ES6新增数据类型 <code>Set</code></h4><p><code>Set</code>类似于数组,但是成员的值都是唯一的,没有重复的值。</p>
|
|
114
|
+
<pre><code>functionuniq(arry) {
|
|
115
|
+
return [...newSet(arry)];
|
|
116
|
+
}
|
|
117
|
+
</code></pre>
|
|
118
|
+
<h4 id="法2-利用-indexOf"><a href="#法2-利用-indexOf" class="headerlink" title="法2: 利用 indexOf"></a>法2: 利用 <code>indexOf</code></h4><pre><code>functionuniq(arry) {
|
|
119
|
+
var result = [];
|
|
120
|
+
for (var i =0; i <arry.length; i++) {
|
|
121
|
+
if (result.indexOf(arry[i]) ===-1) {
|
|
122
|
+
//如 result 中没有 arry[i],则添加到数组中result.push(arry[i])
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
</code></pre>
|
|
128
|
+
<h4 id="法3-利用-includes"><a href="#法3-利用-includes" class="headerlink" title="法3: 利用 includes"></a>法3: 利用 <code>includes</code></h4><pre><code>functionuniq(arry) {
|
|
129
|
+
var result = [];
|
|
130
|
+
for (var i =0; i <arry.length; i++) {
|
|
131
|
+
if (!result.includes(arry[i])) {
|
|
132
|
+
//如 result 中没有 arry[i],则添加到数组中result.push(arry[i])
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
</code></pre>
|
|
138
|
+
<h4 id="法4:利用-reduce"><a href="#法4:利用-reduce" class="headerlink" title="法4:利用 reduce"></a>法4:利用 <code>reduce</code></h4><pre><code>functionuniq(arry) {
|
|
139
|
+
returnarry.reduce((prev, cur) =>prev.includes(cur) ? prev : [...prev, cur], []);
|
|
140
|
+
}
|
|
141
|
+
</code></pre>
|
|
142
|
+
<h4 id="法5:利用-Map"><a href="#法5:利用-Map" class="headerlink" title="法5:利用 Map"></a>法5:利用 <code>Map</code></h4><pre><code>functionuniq(arry) {
|
|
143
|
+
let map =newMap();
|
|
144
|
+
let result =newArray();
|
|
145
|
+
for (let i =0; i <arry.length; i++) {
|
|
146
|
+
if (map.has(arry[i])) {
|
|
147
|
+
map.set(arry[i], true);
|
|
148
|
+
} else {
|
|
149
|
+
map.set(arry[i], false);
|
|
150
|
+
result.push(arry[i]);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
155
|
+
</code></pre>
|
|
156
|
+
<h3 id="23-清除浮动的方法有哪些?"><a href="#23-清除浮动的方法有哪些?" class="headerlink" title="23. 清除浮动的方法有哪些?"></a>23. 清除浮动的方法有哪些?</h3><blockquote>
|
|
157
|
+
<p>当容器的高度为auto,且容器的内容中有浮动(float为left或right)的元素,在这种情况下,容器的高度不能自动伸长以适应内容的高度,使得内容溢出到容器外面而影响(甚至破坏)布局的现象。这个现象叫浮动溢出,为了防止这个现象的出现而进行的CSS处理,就叫CSS清除浮动。</p>
|
|
158
|
+
</blockquote>
|
|
159
|
+
<pre><code><style>.inner {width: 100px;height: 100px;float: left; }</style>
|
|
160
|
+
<divclass='outer'>
|
|
161
|
+
<divclass='inner'></div>
|
|
162
|
+
<divclass='inner'></div>
|
|
163
|
+
<divclass='inner'></div>
|
|
164
|
+
</div>
|
|
165
|
+
</code></pre>
|
|
166
|
+
<h4 id="1-利用-clear-属性"><a href="#1-利用-clear-属性" class="headerlink" title="1. 利用 clear 属性"></a>1. 利用 <code>clear</code> 属性</h4><p>在 <code><div class='outer'></code> 内创建一个空元素,对其设置 <code>clear: both;</code> 的样式。</p>
|
|
167
|
+
<ul>
|
|
168
|
+
<li>优点:简单,代码少,浏览器兼容性好。</li>
|
|
169
|
+
<li>缺点:需要添加大量无语义的html元素,代码不够优雅,后期不容易维护。</li>
|
|
170
|
+
</ul>
|
|
171
|
+
<h4 id="2-利用-clear-属性-伪元素"><a href="#2-利用-clear-属性-伪元素" class="headerlink" title="2. 利用 clear 属性 + 伪元素"></a>2. 利用 <code>clear</code> 属性 + 伪元素</h4><pre><code>.outer:after{
|
|
172
|
+
content: '';
|
|
173
|
+
display: block;
|
|
174
|
+
clear: both;
|
|
175
|
+
visibility: hidden;
|
|
176
|
+
height: 0;
|
|
177
|
+
}
|
|
178
|
+
</code></pre>
|
|
179
|
+
<p>IE8以上和非IE浏览器才支持:after,如果想要支持IE6、7,需要给 <code>outer</code> 元素,设置样式 <code>zoom: 1</code>;</p>
|
|
180
|
+
<h4 id="3-利用-BFC-布局规则"><a href="#3-利用-BFC-布局规则" class="headerlink" title="3. 利用 BFC 布局规则"></a>3. 利用 BFC 布局规则</h4><p>根据 BFC 的规则,计算 BFC 的高度时,浮动元素也参与计算。因此清除浮动,只需要触发一个BFC即可。</p>
|
|
181
|
+
<blockquote>
|
|
182
|
+
<p>可以使用以下方法来触发BFC</p>
|
|
183
|
+
</blockquote>
|
|
184
|
+
<ul>
|
|
185
|
+
<li>position 为 absolute 或 relative</li>
|
|
186
|
+
<li>overflow 不为 visible 的块元素</li>
|
|
187
|
+
<li>display 为 inline-block, table-cell, table-caption</li>
|
|
188
|
+
</ul>
|
|
189
|
+
<p>如:</p>
|
|
190
|
+
<pre><code>.outer {
|
|
191
|
+
overflow: hidden;
|
|
192
|
+
}
|
|
193
|
+
</code></pre>
|
|
194
|
+
<p>注意使用 <code>display: inline-block</code> 会产生间隙。</p>
|
|
195
|
+
<h3 id="24-编写一个通用的柯里化函数-currying"><a href="#24-编写一个通用的柯里化函数-currying" class="headerlink" title="24. 编写一个通用的柯里化函数 currying"></a>24. 编写一个通用的柯里化函数 currying</h3><p>在开始之前,我们首先需要搞清楚函数柯里化的概念。</p>
|
|
196
|
+
<p>函数柯里化是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数而且返回结果的新函数的技术。</p>
|
|
197
|
+
<pre><code>constcurrying= (fn, ...args) =>args.length<fn.length//参数长度不足时,重新柯里化该函数,等待接受新参数? (...arguments) =>currying(fn, ...args, ...arguments)
|
|
198
|
+
//参数长度满足时,执行函数:fn(...args);
|
|
199
|
+
|
|
200
|
+
functionsumFn(a, b, c) {
|
|
201
|
+
return a + b + c;
|
|
202
|
+
}
|
|
203
|
+
var sum =currying(sumFn);
|
|
204
|
+
console.log(sum(2)(3)(5));//10console.log(sum(2, 3, 5));//10console.log(sum(2)(3, 5));//10console.log(sum(2, 3)(5));//10
|
|
205
|
+
</code></pre>
|
|
206
|
+
<blockquote>
|
|
207
|
+
<p>函数柯里化的主要作用:</p>
|
|
208
|
+
</blockquote>
|
|
209
|
+
<ul>
|
|
210
|
+
<li>参数复用</li>
|
|
211
|
+
<li>提前返回 – 返回接受余下的参数且返回结果的新函数</li>
|
|
212
|
+
<li>延迟执行 – 返回新函数,等待执行</li>
|
|
213
|
+
</ul>
|
|
214
|
+
<h3 id="参考文章:"><a href="#参考文章:" class="headerlink" title="参考文章:"></a>参考文章:</h3><p>[1] <a target="_blank" rel="noopener" href="http://www.zhufengpeixun.cn/main/course/index.html">珠峰架构课(墙裂推荐)</a></p>
|
|
215
|
+
<p>[2] <a target="_blank" rel="noopener" href="https://segmentfault.com/a/1190000004865198">CSS-清除浮动</a></p>
|
|
216
|
+
<p>[3] <a target="_blank" rel="noopener" href="https://www.jianshu.com/p/2975c25e4d71">详解JS函数柯里化</a></p>
|
|
217
|
+
<p>[4] <a target="_blank" rel="noopener" href="https://segmentfault.com/a/1190000016418021?utm_source=tag-newest">JavaScript数组去重</a></p>
|
|
218
|
+
<p>谢谢各位小伙伴愿意花费宝贵的时间阅读本文,如果本文给了您一点帮助或者是启发,请不要吝啬你的赞和Star,您的肯定是我前进的最大动力。 <a target="_blank" rel="noopener" href="https://github.com/YvetteLau/Blog">https://github.com/YvetteLau/Blog</a></p>
|
|
219
|
+
</div><iframe src="/donate/?AliPayQR=https://store.xuehuayu.cn/store/public/alipay_qr.jpg&WeChatQR=https://store.xuehuayu.cn/store/public/wechat_qr.png&UnionPayQR=null&GitHub=null&BTCQR=null&BTCKEY=null&PayPal=https://www.paypal.com/paypalme/OrdinaryPerson" style="overflow-x:hidden; overflow-y:hidden; border:0xp none #fff; min-height:240px; width:100%;" frameborder="0" scrolling="no"></iframe><div><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者:</strong>雪花雨</li><li class="post-copyright-link"><strong>本文链接:</strong><a href="/article/38743.html">https://xuehuayu.cn/article/38743.html</a></li><li class="post-copyright-license"><strong>版权声明:</strong>① 标为原创的文章为博主原创,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接。② 转载文章来自网络,已标明出处,<a href="mailto:cainiaoblog@126.com">侵删</a>。</li></ul></div><br><div class="tags"><a href="/tags/%E8%BD%AC%E8%BD%BD/">转载</a></div><div class="post-nav"><a class="pre" href="/article/38423.html">高频面试题深入解析/ 周刊 06</a><a class="next" href="/article/22422.html">高频面试题深入解析/ 周刊 04</a></div><div id="vcomment"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@waline/client@v2/dist/waline.min.css"><script src="//cdn.jsdelivr.net/npm/@waline/client@v2/dist/waline.js"></script><script>const origin = window.location.origin
|
|
220
|
+
const serverURL = origin.includes('cainiaoblog') ? 'https://cmts.cainiaoblog.cn' : 'https://cmts.xuehuayu.cn'
|
|
221
|
+
const locale = {
|
|
222
|
+
placeholder: '请正确填写昵称和邮箱,方便接收回复通知~',
|
|
223
|
+
sofa: '沙发空缺中,还不快抢~',
|
|
224
|
+
};
|
|
225
|
+
Waline.init({
|
|
226
|
+
el: '#vcomment',
|
|
227
|
+
serverURL: serverURL,
|
|
228
|
+
pageSize: '20',
|
|
229
|
+
visitor: true, // 阅读量统计
|
|
230
|
+
requiredMeta: ['nick', 'mail'],
|
|
231
|
+
emoji: [
|
|
232
|
+
'https://cdn.jsdelivr.net/gh/walinejs/emojis@1.0.0/weibo',
|
|
233
|
+
'https://cdn.jsdelivr.net/gh/walinejs/emojis@1.0.0/qq',
|
|
234
|
+
'https://cdn.jsdelivr.net/gh/walinejs/emojis@1.0.0/tw-emoji',
|
|
235
|
+
],
|
|
236
|
+
locale,
|
|
237
|
+
pageview: true
|
|
238
|
+
})
|
|
239
|
+
</script></div></div></div><div class="pure-u-1 pure-u-md-1-4 fixed-search hidden_mid_and_down"><div id="sidebar"><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/search.min.js"></script><div class="widget"><div class="widget-search"><input class="search" type="radio" name="search" value="baidu" id="baidu" checked="checked"/><label class="label" for="baidu" title="百度全站搜索">百度</label><input class="search" type="radio" name="search" value="google" id="google"/><label class="label" for="google" title="谷歌全站搜索">谷歌</label><input class="search" type="radio" name="search" value="self" id="self"/><label class="label" for="self" title="使用站内搜索">站内</label></div><div class="widget" id="search"><form class="search-form" action="//www.baidu.com/baidu" method="get" accept-charset="utf-8" target="_blank"><input type="search" name="word" maxlength="30" placeholder="百度全站搜索"/><input type="hidden" name="si" value="xuehuayu.cn"/><input type="hidden" name="cl" value="3"/><input type="hidden" name="ct" value="2097152"/><input type="hidden" name="s" value="on"/><input class="search-submit" type="submit" value=""/></form></div></div><script>$('input[type=radio][name=search]').change(function() {
|
|
240
|
+
var val = $(this).val()
|
|
241
|
+
var self = '<div class="search-form"><input id="local-search-input" placeholder="站内搜索,首次慢" type="search" name="q" results="0"><input class="search-submit" type="submit" value=""/><div id="local-search-result"></div></div>'
|
|
242
|
+
var google = '<form class="search-form" action="//www.google.com/search" method="get" accept-charset="utf-8" target="_blank"><input type="search" name="q" maxlength="30" placeholder="谷歌全站搜索"><input type="hidden" name="sitesearch" value="xuehuayu.cn"><input class="search-submit" type="submit" value=""/></form>'
|
|
243
|
+
var baidu = '<form class="search-form" action="//www.baidu.com/baidu" method="get" accept-charset="utf-8" target="_blank"><input type="search" name="word" maxlength="30" placeholder="百度全站搜索"><input type="hidden" name="si" value="xuehuayu.cn"><input type="hidden" name="cl" value="3"><input type="hidden" name="ct" value="2097152"><input type="hidden" name="s" value="on"><input class="search-submit" type="submit" value=""/></form>'
|
|
244
|
+
|
|
245
|
+
if (val === 'self') {
|
|
246
|
+
$('#search').html(self)
|
|
247
|
+
var search_path = 'search.xml';
|
|
248
|
+
if (search_path.length == 0) {
|
|
249
|
+
search_path = 'search.xml';
|
|
250
|
+
}
|
|
251
|
+
var path = 'https://cdn.staticaly.com/gh/npljy/npljy.github.io/main/' + search_path;
|
|
252
|
+
searchFunc(path, 'local-search-input', 'local-search-result');
|
|
253
|
+
} else if (val === 'baidu') {
|
|
254
|
+
$('#search').html(baidu)
|
|
255
|
+
} else if (val === 'google') {
|
|
256
|
+
$('#search').html(google)
|
|
257
|
+
}
|
|
258
|
+
})</script><div class="widget widget-wxmp"><img alt="微信公众号" width="100%" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/mp-mini.jpg"/></div><div class="widget widget-categories"><div class="widget-title"><i class="fa fa-folder-o"> 分类</i></div><ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/CDN/">CDN</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/IDE/">IDE</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/git/">git</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/hexo/">hexo</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%85%AC%E5%85%B1%E8%8A%82%E6%97%A5/">公共节日</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%88%86%E4%BA%AB/">分享</a><span class="category-list-count">29</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%89%8D%E7%AB%AF/">前端</a><span class="category-list-count">191</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%89%8D%E7%AB%AF/%E6%B5%8F%E8%A7%88%E5%99%A8/">浏览器</a><span class="category-list-count">1</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%89%8D%E7%AB%AF/%E6%B5%8F%E8%A7%88%E5%99%A8/%E7%BD%91%E7%BB%9C%E5%8D%8F%E8%AE%AE/">网络协议</a><span class="category-list-count">1</span></li></ul></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%8D%9A%E5%AE%A2/">博客</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%B7%A5%E5%85%B7/">工具</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%B9%BF%E5%91%8A%E8%BF%87%E6%BB%A4/">广告过滤</a><span class="category-list-count">8</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%B9%BF%E5%91%8A%E8%BF%87%E6%BB%A4/%E8%A7%86%E9%A2%91%E7%94%B5%E8%A7%86%E5%B0%8F%E8%AF%B4%E6%BC%AB%E7%94%BB%E8%B5%84%E8%AE%AF%E9%9F%B3%E4%B9%90/">视频电视小说漫画资讯音乐</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%AD%A3%E5%88%99/">正则</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%B5%8F%E8%A7%88%E5%99%A8/">浏览器</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%AE%97%E6%B3%95/">算法</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%B5%84%E8%AE%AF/">资讯</a><span class="category-list-count">16</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%BD%BB%E6%9D%BE%E4%B8%80%E5%88%BB/">轻松一刻</a><span class="category-list-count">1</span></li></ul></div><div class="widget widget-tags"><div class="widget-title"><i class="fa fa-star-o"> 标签</i></div><div class="tagcloud"><a href="/tags/%E5%8E%9F%E5%88%9B/" style="font-size: 15px;">原创</a> <a href="/tags/%E8%BD%AC%E8%BD%BD/" style="font-size: 15px;">转载</a> <a href="/tags/%E6%89%8B%E5%86%99/" style="font-size: 15px;">手写</a> <a href="/tags/%E7%96%AB%E6%83%85/" style="font-size: 15px;">疫情</a> <a href="/tags/%E5%A8%B1%E4%B9%90/" style="font-size: 15px;">娱乐</a></div></div><div class="widget widget-recent-posts"><div class="widget-title"><i class="fa fa-file-o"> 最近文章</i></div><ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/article/c5576490.html">面试准备</a></li><li class="post-list-item"><a class="post-list-link" href="/article/84c9ccd6.html">精读《SolidJS》</a></li><li class="post-list-item"><a class="post-list-link" href="/article/53803e6.html">git提交注释规范</a></li><li class="post-list-item"><a class="post-list-link" href="/article/a4145266.html">前端数组拍平flat一行代码</a></li><li class="post-list-item"><a class="post-list-link" href="/article/bedea419.html">一道前端this面试题</a></li><li class="post-list-item"><a class="post-list-link" href="/article/bf7e7421.html">git多账号配置</a></li></ul></div><div class="widget widget-links"><div class="widget-title"><i class="fa fa-external-link"> 友情链接</i></div><ul></ul><a href="https://github.com/npljy?utm_source=xuehuayu.cn" title="GitHub" target="_blank">GitHub</a><ul></ul><a href="https://laonongmin.online?utm_source=xuehuayu.cn" title="看看" target="_blank">看看</a><ul></ul><a href="https://cainiaoblog.cn?utm_source=xuehuayu.cn" title="菜鸟博客" target="_blank">菜鸟博客</a><ul></ul><a href="mailto:cainiaoblog@126.com" title="友链联系" target="_blank">友链联系</a></div></div></div><div class="pure-u-1 pure-u-md-3-4"><div id="footer"><div class="flex-block justify-center align-center flex-wrap"><a class="gxba-link" id="gxba" rel="nofollow" target="_blank" href="http://beian.miit.gov.cn/">京ICP备20007647号-2</a><a class="gaba-link" id="gaba-link" rel="nofollow" target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=11010802031264"><img class="nofancybox" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/gaba.png" alt=""/><span id="gaba">京公网安备 11010802031264号</span><span style="padding-right: 10px;"></span></a><span>Copyright © 2023 </span><a href="/." rel="nofollow">前端壹菜鸟. </a><script>(function(){
|
|
259
|
+
var cnb =window.location.origin.indexOf('cainiaoblog') !==-1
|
|
260
|
+
if (cnb) {
|
|
261
|
+
var gxba =document.getElementById('gxba')
|
|
262
|
+
var gaba =document.getElementById('gaba')
|
|
263
|
+
var gabaLink =document.getElementById('gaba-link')
|
|
264
|
+
gxba.innerText ='京ICP备20007647号-1'
|
|
265
|
+
gaba.innerText ='京公网安备 11010802031254号'
|
|
266
|
+
gabaLink.setAttribute('href','http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=11010802031254')
|
|
267
|
+
}
|
|
268
|
+
})()</script></div><div class="flex-block justify-center align-center flex-wrap"><a rel="nofollow" target="_blank" href="https://v6.51.la/s/Guz3lwHCsVme9Cu"><div class="busuanzi_container" id="busuanzi_container"><span class="busuanzi_container_site_pv" id="busuanzi_container_site_pv">访问量:<span id="busuanzi_value_site_pv"><i class="fa fa-spinner"></i></span></span><span class="busuanzi_container_site_uv" id="busuanzi_container_site_uv">访客数:<span id="busuanzi_value_site_uv"><i class="fa fa-spinner"></i></span></span><span style="padding-right: 10px;"></span></div></a><a rel="nofollow" target="_blank" href="https://www.upyun.com/?utm_source=lianmeng&utm_medium=referral"><span style="font-weight:bold;letter-spacing: 1px;">本网站由 </span><img class="nofancybox" height="32" style="vertical-align: middle" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/upy_logo.min.svg" alt=""/><span style="font-weight:bold;letter-spacing: 1px;">提供CDN加速/云存储服务</span></a><span style="padding-right: 10px;"></span><a rel="nofollow" style="font-size:18px;font-weight:bold;vertical-align: middle" target="_blank" title="注册就送代金券可直接使用" href="https://console.upyun.com/register/?invite=HyDsjZHIL">注册</a></div></div></div></div><a class="show" id="rocket" href="#top"></a><div class="darkmode-toggle">🌓</div><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/totop.min.js" async></script><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/dark.min.js" async></script><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/codeblock-resizer.min.js"></script><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/smartresize.min.js"></script></div></body></html>
|