-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
81 lines (73 loc) · 13.9 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>module.exports和exports的区别 | Clearives</title>
<meta name="generator" content="VuePress 1.9.10">
<link rel="icon" href="/favicon.ico">
<meta name="description" content="前端博客clearives.github.io是基于vuepress搭建的前端技术分享博客,有js、node、react、vue等前端技术分享">
<meta name="keywords" content="前端开发,前端技术,html,js,JavaScript,node,react,vue,前端博客,clearives,vuepress">
<link rel="preload" href="/assets/css/0.styles.ccf9e52d.css" as="style"><link rel="preload" href="/assets/js/app.a82c2ceb.js" as="script"><link rel="preload" href="/assets/js/2.2ec38cce.js" as="script"><link rel="preload" href="/assets/js/1.0eb2d8a2.js" as="script"><link rel="preload" href="/assets/js/42.e58bbe9e.js" as="script"><link rel="preload" href="/assets/js/19.d8fa848e.js" as="script"><link rel="prefetch" href="/assets/js/10.63e25234.js"><link rel="prefetch" href="/assets/js/11.f9337695.js"><link rel="prefetch" href="/assets/js/12.f76ea8f7.js"><link rel="prefetch" href="/assets/js/13.70147e98.js"><link rel="prefetch" href="/assets/js/14.985749a8.js"><link rel="prefetch" href="/assets/js/15.3a162e02.js"><link rel="prefetch" href="/assets/js/16.50c541be.js"><link rel="prefetch" href="/assets/js/17.fff01f94.js"><link rel="prefetch" href="/assets/js/18.aa896f4f.js"><link rel="prefetch" href="/assets/js/20.2a87130e.js"><link rel="prefetch" href="/assets/js/21.cc57d586.js"><link rel="prefetch" href="/assets/js/22.a015931b.js"><link rel="prefetch" href="/assets/js/23.aebdeb23.js"><link rel="prefetch" href="/assets/js/24.d6fac3e0.js"><link rel="prefetch" href="/assets/js/25.1d84d454.js"><link rel="prefetch" href="/assets/js/26.2c0a959d.js"><link rel="prefetch" href="/assets/js/27.c4abfccf.js"><link rel="prefetch" href="/assets/js/28.391b531f.js"><link rel="prefetch" href="/assets/js/29.f833303b.js"><link rel="prefetch" href="/assets/js/3.16d5a572.js"><link rel="prefetch" href="/assets/js/30.474b40f7.js"><link rel="prefetch" href="/assets/js/31.c070441b.js"><link rel="prefetch" href="/assets/js/32.5d456f3a.js"><link rel="prefetch" href="/assets/js/33.3d59c3a5.js"><link rel="prefetch" href="/assets/js/34.4ef8aa2b.js"><link rel="prefetch" href="/assets/js/35.69640a18.js"><link rel="prefetch" href="/assets/js/36.0e4c7e69.js"><link rel="prefetch" href="/assets/js/37.0eaf8656.js"><link rel="prefetch" href="/assets/js/38.b08624a0.js"><link rel="prefetch" href="/assets/js/39.d1c096b0.js"><link rel="prefetch" href="/assets/js/4.91114f18.js"><link rel="prefetch" href="/assets/js/40.c2ad95a9.js"><link rel="prefetch" href="/assets/js/41.7bd65731.js"><link rel="prefetch" href="/assets/js/43.87b68bc5.js"><link rel="prefetch" href="/assets/js/44.1cc195bc.js"><link rel="prefetch" href="/assets/js/45.8e911753.js"><link rel="prefetch" href="/assets/js/46.f3da31ab.js"><link rel="prefetch" href="/assets/js/47.929523a9.js"><link rel="prefetch" href="/assets/js/48.2b6531d4.js"><link rel="prefetch" href="/assets/js/49.09ce8ff7.js"><link rel="prefetch" href="/assets/js/5.06da80d1.js"><link rel="prefetch" href="/assets/js/50.d6c3515b.js"><link rel="prefetch" href="/assets/js/6.a76778da.js"><link rel="prefetch" href="/assets/js/7.e9104bde.js"><link rel="prefetch" href="/assets/js/vendors~docsearch.a86ae6f6.js">
<link rel="stylesheet" href="/assets/css/0.styles.ccf9e52d.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">Clearives</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
首页
</a></div><div class="nav-item"><a href="/frontend/" class="nav-link">
前端知识
</a></div><div class="nav-item"><a href="/node/" class="nav-link">
Nodejs
</a></div><div class="nav-item"><a href="/package/" class="nav-link">
前端框架
</a></div><div class="nav-item"><a href="/daily/record/" class="nav-link">
日记本
</a></div><div class="nav-item"><a href="/about/" class="nav-link">
关于我
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
首页
</a></div><div class="nav-item"><a href="/frontend/" class="nav-link">
前端知识
</a></div><div class="nav-item"><a href="/node/" class="nav-link">
Nodejs
</a></div><div class="nav-item"><a href="/package/" class="nav-link">
前端框架
</a></div><div class="nav-item"><a href="/daily/record/" class="nav-link">
日记本
</a></div><div class="nav-item"><a href="/about/" class="nav-link">
关于我
</a></div> <!----></nav> <ul class="sidebar-links"><li><a href="/node/" class="sidebar-link">Node-js</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>学习笔记</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/page/BASE64-encryption-decryption/" class="sidebar-link">BASE64加/解密</a></li><li><a href="/page/module.exports-and-exports/" aria-current="page" class="active sidebar-link">module.exports和exports的区别</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/page/module.exports-and-exports/#module和exports" class="sidebar-link">module和exports</a></li><li class="sidebar-sub-header"><a href="/page/module.exports-and-exports/#require" class="sidebar-link">require</a></li></ul></li><li><a href="/page/Multiple-asynchronous-collaboration/" class="sidebar-link">多异步协作</a></li><li><a href="/page/Asynchronous-and-event-loops/" class="sidebar-link">异步和事件触发</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><ul><li><a href="#moduleexports%E5%92%8Cexports%E7%9A%84%E5%8C%BA%E5%88%AB">module.exports和exports的区别</a> <ul><li><a href="#module%E5%92%8Cexports">module和exports</a></li> <li><a href="#require">require</a></li></ul></li></ul> <h1 id="module-exports和exports的区别"><a href="#module-exports和exports的区别" class="header-anchor">#</a> module.exports和exports的区别</h1> <blockquote><p>Node模块允许你从被引入文件中选择要暴露给程序的函数和变量。如果模块返回的函数或变 量不止一个,那它可以通过设定exports对象的属性来指明它们。但如果模块只返回一个函数或变量,则可以设定module.exports属性。</p></blockquote> <h2 id="module和exports"><a href="#module和exports" class="header-anchor">#</a> module和exports</h2> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// demo.js</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>module<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>exports<span class="token punctuation">)</span>
<span class="token comment">// 输出</span>
Module <span class="token punctuation">{</span>
<span class="token literal-property property">id</span><span class="token operator">:</span> <span class="token string">'.'</span><span class="token punctuation">,</span>
<span class="token literal-property property">exports</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">parent</span><span class="token operator">:</span> <span class="token keyword">null</span><span class="token punctuation">,</span>
<span class="token literal-property property">filename</span><span class="token operator">:</span>
<span class="token string">'.../demo1.js'</span><span class="token punctuation">,</span>
<span class="token literal-property property">loaded</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
<span class="token literal-property property">children</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token literal-property property">paths</span><span class="token operator">:</span>
<span class="token punctuation">[</span> <span class="token operator">...</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p>module.exports和exports一开始都是一个空对象{},本质上这两个指向的是同一块内存,</p> <h2 id="require"><a href="#require" class="header-anchor">#</a> require</h2> <blockquote><p>require引入的对象本质上是module.exports。这就产生了一个问题,当 module.exports和exports指向的不是同一块内存时,exports的内容就会失效。</p></blockquote> <p>最终在程序里导出的是module.exports。exports只是对module.exports的一个全 局引用,最初被定义为一个可以添加属性的空对象。如果把exports设定为别的,就打破了module.exports和exports之间的 引用关系。可是因为真正导出的是module.exports,那样exports就不能用了,因为 它不再指向module.exports了。如果你想维持那个链接,可以像下面这样让 module.exports再次引用exports。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// person.js</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'clearives'</span><span class="token punctuation">}</span>
exports <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'Mr、z'</span><span class="token punctuation">}</span>
<span class="token comment">// demo.js</span>
<span class="token keyword">const</span> person <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./person'</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>person<span class="token punctuation">)</span> <span class="token comment">// {name: 'clearives'}</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> exports <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>参考:
<a href="https://github.com/Clearives/clearives.github.io" target="" rel="noopener noreferrer">Node.js实战</a></p></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">更新于:</span> <span class="time">7/8/2020, 7:46:15 AM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
←
<a href="/page/BASE64-encryption-decryption/" class="prev">
BASE64加/解密
</a></span> <span class="next"><a href="/page/Multiple-asynchronous-collaboration/">
多异步协作
</a>
→
</span></p></div> </main></div><div class="global-ui"><!----><div></div><!----></div></div>
<script src="/assets/js/app.a82c2ceb.js" defer></script><script src="/assets/js/2.2ec38cce.js" defer></script><script src="/assets/js/1.0eb2d8a2.js" defer></script><script src="/assets/js/42.e58bbe9e.js" defer></script><script src="/assets/js/19.d8fa848e.js" defer></script>
</body>
</html>