<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Logan</title>
    <description>logan个人博客:metal:</description>
    <link>https://logan70.github.io/</link>
    <atom:link href="https://logan70.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Tue, 13 Nov 2018 15:17:13 +0000</pubDate>
    <lastBuildDate>Tue, 13 Nov 2018 15:17:13 +0000</lastBuildDate>
    <generator>Jekyll v3.7.4</generator>
    
      <item>
        <title>JavaScript基础-原型及原型链</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#一引用类型皆为对象&quot; id=&quot;markdown-toc-一引用类型皆为对象&quot;&gt;一、引用类型皆为对象&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#二原型和原型链是什么&quot; id=&quot;markdown-toc-二原型和原型链是什么&quot;&gt;二、原型和原型链是什么&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#三创建对象&quot; id=&quot;markdown-toc-三创建对象&quot;&gt;三、创建对象&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#四__proto__与prototype&quot; id=&quot;markdown-toc-四__proto__与prototype&quot;&gt;四、&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;与&lt;code class=&quot;highlighter-rouge&quot;&gt;prototype&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#五原型链顶层&quot; id=&quot;markdown-toc-五原型链顶层&quot;&gt;五、原型链顶层&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#六constructor&quot; id=&quot;markdown-toc-六constructor&quot;&gt;六、&lt;code class=&quot;highlighter-rouge&quot;&gt;constructor&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#七函数对象的原型链&quot; id=&quot;markdown-toc-七函数对象的原型链&quot;&gt;七、函数对象的原型链&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#八小试牛刀&quot; id=&quot;markdown-toc-八小试牛刀&quot;&gt;八、小试牛刀&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#九举一反三&quot; id=&quot;markdown-toc-九举一反三&quot;&gt;九、举一反三&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1-instanceof操作符&quot; id=&quot;markdown-toc-1-instanceof操作符&quot;&gt;1. &lt;code class=&quot;highlighter-rouge&quot;&gt;instanceof&lt;/code&gt;操作符&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2-objectcreate&quot; id=&quot;markdown-toc-2-objectcreate&quot;&gt;2. &lt;code class=&quot;highlighter-rouge&quot;&gt;Object.create&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#3-new操作符&quot; id=&quot;markdown-toc-3-new操作符&quot;&gt;3. &lt;code class=&quot;highlighter-rouge&quot;&gt;new&lt;/code&gt;操作符&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#4-function__proto__--functionprototype&quot; id=&quot;markdown-toc-4-function__proto__--functionprototype&quot;&gt;4. &lt;code class=&quot;highlighter-rouge&quot;&gt;Function.__proto__ === Function.prototype&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#5-真的是继承吗&quot; id=&quot;markdown-toc-5-真的是继承吗&quot;&gt;5. 真的是继承吗？&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#十参考文章&quot; id=&quot;markdown-toc-十参考文章&quot;&gt;十、参考文章&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

说到JavaScript的原型和原型链，相关文章已有不少，但是大都晦涩难懂。本文将换一个角度出发，先理解原型和原型链是什么，有什么作用，再去分析那些令人头疼的关系。

&lt;h1 id=&quot;一引用类型皆为对象&quot;&gt;一、引用类型皆为对象&lt;/h1&gt;

原型和原型链都是来源于对象而服务于对象的概念，所以我们要先明确一点：

&lt;strong&gt;JavaScript中一切引用类型都是对象，对象就是属性的集合。&lt;/strong&gt;

&lt;code class=&quot;highlighter-rouge&quot;&gt;Array类型&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;Function类型&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;Object类型&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;Date类型&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;RegExp类型&lt;/code&gt;等都是引用类型。

也就是说数组是对象、函数是对象、正则是对象、对象还是对象。

&lt;img src=&quot;https://user-gold-cdn.xitu.io/2018/11/9/166f6326edc99c76?w=435&amp;amp;h=184&amp;amp;f=png&amp;amp;s=17467&quot; alt=&quot;&quot; /&gt;

&lt;strong&gt;总结一下：数组是对象、函数是对象、对象还是对象，只要是引用类型，就都是对象。&lt;/strong&gt;

&lt;h1 id=&quot;二原型和原型链是什么&quot;&gt;二、原型和原型链是什么&lt;/h1&gt;

上面我们说到对象就是属性（property）的集合，有人可能要问不是还有方法吗？其实方法也是一种属性，因为它也是&lt;code class=&quot;highlighter-rouge&quot;&gt;键值对&lt;/code&gt;的表现形式，具体见下图。

&lt;img src=&quot;https://user-gold-cdn.xitu.io/2018/11/9/166f64bf36e55099?w=347&amp;amp;h=182&amp;amp;f=png&amp;amp;s=30826&quot; alt=&quot;&quot; /&gt;

可以看到&lt;code class=&quot;highlighter-rouge&quot;&gt;obj&lt;/code&gt;上确实多了一个&lt;code class=&quot;highlighter-rouge&quot;&gt;sayHello&lt;/code&gt;的属性，值为一个函数，但是问题来了，&lt;code class=&quot;highlighter-rouge&quot;&gt;obj&lt;/code&gt;上面并没有&lt;code class=&quot;highlighter-rouge&quot;&gt;hasOwnProperty&lt;/code&gt;这个方法，为什么我们可以调用呢？这就引出了&lt;strong&gt;原型&lt;/strong&gt;。

每一个对象从被创建开始就和另一个对象关联，从另一个对象上继承其属性，这个&lt;code class=&quot;highlighter-rouge&quot;&gt;另一个对象&lt;/code&gt;就是&lt;strong&gt;原型&lt;/strong&gt;。

那么继承原型的属性是怎么实现的呢？当访问一个对象的属性时，先在对象的本身找，找不到就去对象的原型上找，如果还是找不到，就去对象的原型（原型也是对象，也有它自己的原型）的原型上找，如此继续，直到找到为止，或者查找到最顶层的原型对象中也没有找到，就结束查找，返回&lt;code class=&quot;highlighter-rouge&quot;&gt;undefined&lt;/code&gt;。这条由对象及其原型组成的链就叫做&lt;strong&gt;原型链&lt;/strong&gt;。

到现在大家明白为什么数组都可以使用&lt;code class=&quot;highlighter-rouge&quot;&gt;push&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;slice&lt;/code&gt;等方法，函数可以使用&lt;code class=&quot;highlighter-rouge&quot;&gt;call&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;bind&lt;/code&gt;等方法了吧，因为在它们的原型链上找到了对应的方法。

现在我们已经初步理解了原型和原型链。没错，我认为初学者不应该从那些包含&lt;code class=&quot;highlighter-rouge&quot;&gt;proto&lt;/code&gt;的词和各种指来指去的箭头开始理解，而是理解其本质及存在的意义。那些让人头疼的东西应该在我们初步理解原型和原型链后用来深入理解及拓展。

OK，&lt;strong&gt;总结一下&lt;/strong&gt;：

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;原型存在的意义就是组成原型链&lt;/strong&gt;：引用类型皆对象，每个对象都有原型，原型也是对象，也有它自己的原型，一层一层，组成原型链。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;原型链存在的意义就是继承&lt;/strong&gt;：访问对象属性时，在对象本身找不到，就在原型链上一层一层找。说白了就是一个对象可以访问其他对象中的属性。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;继承存在的意义就是属性共享&lt;/strong&gt;：好处有二：一是代码重用，字面意思；二是可扩展，不同对象可能继承相同的属性，也可以定义只属于自己的属性。&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;三创建对象&quot;&gt;三、创建对象&lt;/h1&gt;

对象的创建方式主要有两种，一种是&lt;code class=&quot;highlighter-rouge&quot;&gt;new&lt;/code&gt;操作符后跟函数调用，另一种是字面量表示法。

目前我们现在可以理解为：所有对象都是由&lt;code class=&quot;highlighter-rouge&quot;&gt;new&lt;/code&gt;操作符后跟函数调用来创建的，字面量表示法只是语法糖（即本质也是&lt;code class=&quot;highlighter-rouge&quot;&gt;new&lt;/code&gt;，功能不变，使用更简洁）。

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// new操作符后跟函数调用&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 字面量表示法&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 等同于&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 等同于&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;highlighter-rouge&quot;&gt;Object&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;Array&lt;/code&gt;等称为构造函数，不要怕这个概念，构造函数和普通函数并没有什么不同，只是由于这些函数常被用来跟在&lt;code class=&quot;highlighter-rouge&quot;&gt;new&lt;/code&gt;后面创建对象。&lt;code class=&quot;highlighter-rouge&quot;&gt;new&lt;/code&gt;后面调用一个空函数也会返回一个对象，&lt;strong&gt;任何一个函数都可以当做构造函数&lt;/strong&gt;。

所以构造函数更合理的理解应该是&lt;code class=&quot;highlighter-rouge&quot;&gt;函数的构造调用&lt;/code&gt;。

&lt;code class=&quot;highlighter-rouge&quot;&gt;Number&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;String&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;Boolean&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;Array&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;Object&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;Function&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;Date&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;RegExp&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;Error&lt;/code&gt;这些都是函数，而且是原生构造函数，在运行时会自动出现在执行环境中。可以使用&lt;code class=&quot;highlighter-rouge&quot;&gt;typeof Function === 'function'&lt;/code&gt;在控制台验证下。

构造函数是为了创建特定类型的对象，这些通过同一构造函数创建的对象有相同原型，共享某些方法。举个例子，所有的数组都可以调用&lt;code class=&quot;highlighter-rouge&quot;&gt;push&lt;/code&gt;方法，因为它们有相同原型。

我们来自己实现一个构造函数：

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 惯例，构造函数应以大写字母开头&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 函数内this指向构造的对象&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 构造一个name属性&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 构造一个sayName方法&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;sayName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 使用自定义构造函数Person创建对象&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;person&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'logan'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;sayName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 输出：logan&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;总结一下&lt;/strong&gt;：&lt;strong&gt;构造函数用来创建对象，同一构造函数创建的对象，其原型相同。&lt;/strong&gt;

&lt;h1 id=&quot;四__proto__与prototype&quot;&gt;四、&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;与&lt;code class=&quot;highlighter-rouge&quot;&gt;prototype&lt;/code&gt;&lt;/h1&gt;

万物逃不开真香定律，初步了解了相关知识，我们也要试着来理解一下这些头疼的单词，并且看一下指来指去的箭头了。

上面总结过，每个对象都有原型，那么我们怎么获取到一个对象的原型呢？那就是对象的&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;属性，指向对象的原型。

上面也总结过，引用类型皆对象，所以引用类型都有&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;属性，对象有&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;属性，函数有&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;属性，数组也有&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;属性，只要是引用类型，就有&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;属性，都指向它们各自的原型对象。

&lt;img src=&quot;https://user-gold-cdn.xitu.io/2018/11/9/166f828b681072eb?w=599&amp;amp;h=274&amp;amp;f=png&amp;amp;s=21131&quot; alt=&quot;&quot; /&gt;

&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;属性虽然在ECMAScript 6语言规范中标准化，但是不推荐被使用，现在更推荐使用&lt;code class=&quot;highlighter-rouge&quot;&gt;Object.getPrototypeOf&lt;/code&gt;，&lt;code class=&quot;highlighter-rouge&quot;&gt;Object.getPrototypeOf(obj)&lt;/code&gt;也可以获取到&lt;code class=&quot;highlighter-rouge&quot;&gt;obj&lt;/code&gt;对象的原型。本文中使用&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;只是用来深入理解原型和原型链。

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getPrototypeOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

上面还总结过，构造函数是为了创建特定类型的对象，那如果我想让&lt;code class=&quot;highlighter-rouge&quot;&gt;Person&lt;/code&gt;这个构造函数创建的对象都共享一个方法，总不能像下面这样吧：

&lt;strong&gt;错误示范&lt;/strong&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 调用构造函数Person创建一个新对象personA&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;personA&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'张三'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 在personA的原型上添加一个方法，以供之后Person创建的对象所共享&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;personA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;eat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'吃东西'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;personB&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'李四'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;personB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;eat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 输出：吃东西&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

但是每次要修改一类对象的原型对象，都去创建一个新的对象实例，然后访问其原型对象并添加or修改属性总觉得多此一举。既然构造函数创建的对象实例的原型对象都是同一个，那么构造函数和其构造出的对象实例的原型对象之间有联系就完美了。

&lt;img src=&quot;https://user-gold-cdn.xitu.io/2018/11/9/166f82804842244b?w=602&amp;amp;h=291&amp;amp;f=png&amp;amp;s=22608&quot; alt=&quot;&quot; /&gt;

这个联系就是&lt;code class=&quot;highlighter-rouge&quot;&gt;prototype&lt;/code&gt;。每个函数拥有&lt;code class=&quot;highlighter-rouge&quot;&gt;prototype&lt;/code&gt;属性，指向使用&lt;code class=&quot;highlighter-rouge&quot;&gt;new&lt;/code&gt;操作符和该函数创建的对象实例的原型对象。

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;img src=&quot;https://user-gold-cdn.xitu.io/2018/11/9/166f829f2814bb9a?w=631&amp;amp;h=286&amp;amp;f=png&amp;amp;s=24769&quot; alt=&quot;&quot; /&gt;

看到这里我们就明白了，如果想让&lt;code class=&quot;highlighter-rouge&quot;&gt;Person&lt;/code&gt;创建出的对象实例共享属性，应该这样写：

&lt;strong&gt;正确示范&lt;/strong&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;drink&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'喝东西'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;personA&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'张三'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;personB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;drink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 输出：喝东西&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

OK，惯例，&lt;strong&gt;总结一下&lt;/strong&gt;：

&lt;ol&gt;
  &lt;li&gt;对象有&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;属性，函数有&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;属性，数组也有&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;属性，只要是引用类型，就有&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;属性，指向其原型。&lt;/li&gt;
  &lt;li&gt;只有函数有&lt;code class=&quot;highlighter-rouge&quot;&gt;prototype&lt;/code&gt;属性，只有函数有&lt;code class=&quot;highlighter-rouge&quot;&gt;prototype&lt;/code&gt;属性，只有函数有&lt;code class=&quot;highlighter-rouge&quot;&gt;prototype&lt;/code&gt;属性，指向&lt;code class=&quot;highlighter-rouge&quot;&gt;new&lt;/code&gt;操作符加调用该函数创建的对象实例的原型对象。&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;五原型链顶层&quot;&gt;五、原型链顶层&lt;/h1&gt;

原型链之所以叫原型链，而不叫原型环，说明它是有始有终的，那么原型链的顶层是什么呢？

拿我们的&lt;code class=&quot;highlighter-rouge&quot;&gt;person&lt;/code&gt;对象来看，它的原型对象，很简单

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 1. person的原型对象&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

接着往上找，&lt;code class=&quot;highlighter-rouge&quot;&gt;Person.prototype&lt;/code&gt;也是一个普通对象，可以理解为&lt;code class=&quot;highlighter-rouge&quot;&gt;Object&lt;/code&gt;构造函数创建的，所以得出下面结论，

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 2. Person.prototype的原型对象&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;highlighter-rouge&quot;&gt;Object.prototype&lt;/code&gt;也是一个对象，那么它的原型呢？这里比较特殊，切记！！！

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

我们就可以换个方式描述下&lt;strong&gt;原型链&lt;/strong&gt;：由&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;串连起来的直到&lt;code class=&quot;highlighter-rouge&quot;&gt;Object.prototype.__proto__&lt;/code&gt;（为&lt;code class=&quot;highlighter-rouge&quot;&gt;null&lt;/code&gt;）的链就是原型链。

在上面内容的基础之上，我们来模拟一下js引擎读取对象属性：

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;getProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;propName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// 在对象本身查找&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;hasOwnProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;propName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;propName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!==&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// 如果对象有原型，则在原型上递归查找&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;getProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;propName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// 直到找到Object.prototype，Object.prototype.__proto__为null，返回undefined&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;undefined&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;六constructor&quot;&gt;六、&lt;code class=&quot;highlighter-rouge&quot;&gt;constructor&lt;/code&gt;&lt;/h1&gt;

回忆一下之前的描述，构造函数都有一个&lt;code class=&quot;highlighter-rouge&quot;&gt;prototype&lt;/code&gt;属性，指向使用这个构造函数创建的对象实例的&lt;strong&gt;原型对象&lt;/strong&gt;。

这个&lt;strong&gt;原型对象&lt;/strong&gt;中默认有一个&lt;code class=&quot;highlighter-rouge&quot;&gt;constructor&lt;/code&gt;属性，指回该构造函数。

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;constructor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

之所以开头不说，是因为这个属性对我们理解原型及原型链并无太大帮助，反而容易混淆。

&lt;img src=&quot;https://user-gold-cdn.xitu.io/2018/11/9/166f83af5e67ebcf?w=625&amp;amp;h=288&amp;amp;f=png&amp;amp;s=26149&quot; alt=&quot;&quot; /&gt;

&lt;h1 id=&quot;七函数对象的原型链&quot;&gt;七、函数对象的原型链&lt;/h1&gt;

之前提到过引用类型皆对象，函数也是对象，那么函数对象的原型链是怎么样的呢？

对象都是被构造函数创建的，函数对象的构造函数就是&lt;code class=&quot;highlighter-rouge&quot;&gt;Function&lt;/code&gt;，注意这里&lt;code class=&quot;highlighter-rouge&quot;&gt;F&lt;/code&gt;是大写。

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 函数（包括原生构造函数）的原型对象为Function.prototype&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;highlighter-rouge&quot;&gt;Function.prototype&lt;/code&gt;也是一个普通对象，所以&lt;code class=&quot;highlighter-rouge&quot;&gt;Function.prototype.__proto__ === Object.prototype&lt;/code&gt;

这里有一个特例，&lt;code class=&quot;highlighter-rouge&quot;&gt;Function&lt;/code&gt;的&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;属性指向&lt;code class=&quot;highlighter-rouge&quot;&gt;Function.prototype&lt;/code&gt;。

&lt;strong&gt;总结一下：函数都是由&lt;code class=&quot;highlighter-rouge&quot;&gt;Function&lt;/code&gt;原生构造函数创建的，所以函数的&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;属性指向&lt;code class=&quot;highlighter-rouge&quot;&gt;Function&lt;/code&gt;的&lt;code class=&quot;highlighter-rouge&quot;&gt;prototype&lt;/code&gt;属性&lt;/strong&gt;

&lt;h1 id=&quot;八小试牛刀&quot;&gt;八、小试牛刀&lt;/h1&gt;

真香警告！

&lt;img src=&quot;https://user-gold-cdn.xitu.io/2018/11/9/166f859ad7cc4b0b?w=488&amp;amp;h=590&amp;amp;f=png&amp;amp;s=151684&quot; alt=&quot;&quot; /&gt;

有点乱？没事，我们先将之前的知识都总结一下，然后慢慢分析此图：

&lt;strong&gt;知识点&lt;/strong&gt;

&lt;ol&gt;
  &lt;li&gt;引用类型都是对象，每个对象都有原型对象。&lt;/li&gt;
  &lt;li&gt;对象都是由构造函数创建，对象的&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;属性指向其原型对象，构造函数的&lt;code class=&quot;highlighter-rouge&quot;&gt;prototype&lt;/code&gt;属性指向其创建的对象实例的原型对象，所以对象的&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;属性等于创建它的构造函数的&lt;code class=&quot;highlighter-rouge&quot;&gt;prototype&lt;/code&gt;属性。&lt;/li&gt;
  &lt;li&gt;所有通过字面量表示法创建的普通对象的构造函数为&lt;code class=&quot;highlighter-rouge&quot;&gt;Object&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;所有原型对象都是普通对象，构造函数为&lt;code class=&quot;highlighter-rouge&quot;&gt;Object&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;所有函数的构造函数是&lt;code class=&quot;highlighter-rouge&quot;&gt;Function&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Object.prototype&lt;/code&gt;没有原型对象&lt;/li&gt;
&lt;/ol&gt;

OK，我们根据以上六点总结来分析上图，先从左上角的&lt;code class=&quot;highlighter-rouge&quot;&gt;f1&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;f2&lt;/code&gt;入手：

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// f1、f2都是通过new Foo()创建的对象，构造函数为Foo，所以有&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;f1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Foo.prototype为普通对象，构造函数为Object，所以有&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Object.prototype没有原型对象&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

然后对构造函数&lt;code class=&quot;highlighter-rouge&quot;&gt;Foo&lt;/code&gt;下手：

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Foo是个函数对象，构造函数为Function&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Function.prototype为普通对象，构造函数为Object，所以有&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

接着对原生构造函数&lt;code class=&quot;highlighter-rouge&quot;&gt;Object&lt;/code&gt;创建的&lt;code class=&quot;highlighter-rouge&quot;&gt;o1&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;o2&lt;/code&gt;下手：

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// o1、o2构造函数为Object&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;o1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

最后对原生构造函数&lt;code class=&quot;highlighter-rouge&quot;&gt;Object&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;Function&lt;/code&gt;下手：

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 原生构造函数也是函数对象，其构造函数为Function&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 特例&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

分析完毕，也没有想象中那么复杂是吧。

如果有内容引起不适，建议从头看一遍，或者去看看参考文章内的文章。

&lt;h1 id=&quot;九举一反三&quot;&gt;九、举一反三&lt;/h1&gt;

&lt;h2 id=&quot;1-instanceof操作符&quot;&gt;1. &lt;code class=&quot;highlighter-rouge&quot;&gt;instanceof&lt;/code&gt;操作符&lt;/h2&gt;

平常我们判断一个变量的类型会使用&lt;code class=&quot;highlighter-rouge&quot;&gt;typeof&lt;/code&gt;运算符，但是引用类型并不适用，除了函数对象会返回&lt;code class=&quot;highlighter-rouge&quot;&gt;function&lt;/code&gt;外，其他都返回&lt;code class=&quot;highlighter-rouge&quot;&gt;object&lt;/code&gt;。我们想要知道一个对象的具体类型，就需要使用到&lt;code class=&quot;highlighter-rouge&quot;&gt;instanceof&lt;/code&gt;。

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

为什么&lt;code class=&quot;highlighter-rouge&quot;&gt;fn instanceof Object&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;arr instanceof Object&lt;/code&gt;都返回&lt;code class=&quot;highlighter-rouge&quot;&gt;true&lt;/code&gt;呢？我们来看一下MDN上对于&lt;code class=&quot;highlighter-rouge&quot;&gt;instanceof&lt;/code&gt;运算符的描述：

&lt;blockquote&gt;
  instanceof运算符用于测试构造函数的prototype属性是否出现在对象的原型链中的任何位置
&lt;/blockquote&gt;

也就是说&lt;code class=&quot;highlighter-rouge&quot;&gt;instanceof&lt;/code&gt;操作符左边是一个对象，右边是一个构造函数，在左边对象的原型链上查找，知道找到右边构造函数的prototype属性就返回&lt;code class=&quot;highlighter-rouge&quot;&gt;true&lt;/code&gt;，或者查找到顶层&lt;code class=&quot;highlighter-rouge&quot;&gt;null&lt;/code&gt;（也就是&lt;code class=&quot;highlighter-rouge&quot;&gt;Object.prototype.__proto__&lt;/code&gt;），就返回&lt;code class=&quot;highlighter-rouge&quot;&gt;false&lt;/code&gt;。
我们模拟实现一下：

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;instanceOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Constructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// obj 表示左边的对象，Constructor表示右边的构造函数&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;rightP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Constructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 取构造函数显示原型&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;leftP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 取对象隐式原型&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// 到达原型链顶层还未找到则返回false&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;leftP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// 对象实例的隐式原型等于构造函数显示原型则返回true&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;leftP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;rightP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// 查找原型链上一层&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;instanceOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

现在就可以解释一些比较令人费解的结果了：

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//true&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 1. fn.__proto__ === Function.prototype&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 2. fn.__proto__.__proto__ === Function.prototype.__proto__ === Object.prototype&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//true&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 1. arr.__proto__ === Array.prototype&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 2. arr.__proto__.__proto__ === Array.prototype.__proto__ === Object.prototype&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 1. Object.__proto__ === Function.prototype&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 2. Object.__proto__.__proto__ === Function.prototype.__proto__ === Object.prototype&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Function&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Function.__proto__ === Function.prototype&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;总结一下：&lt;code class=&quot;highlighter-rouge&quot;&gt;instanceof&lt;/code&gt;运算符用于检查右边构造函数的&lt;code class=&quot;highlighter-rouge&quot;&gt;prototype&lt;/code&gt;属性是否出现在左边对象的原型链中的任何位置。其实它表示的是一种原型链继承的关系。&lt;/strong&gt;

&lt;h2 id=&quot;2-objectcreate&quot;&gt;2. &lt;code class=&quot;highlighter-rouge&quot;&gt;Object.create&lt;/code&gt;&lt;/h2&gt;

之前说对象的创建方式主要有两种，一种是&lt;code class=&quot;highlighter-rouge&quot;&gt;new&lt;/code&gt;操作符后跟函数调用，另一种是字面量表示法。

其实还有第三种就是ES5提供的&lt;code class=&quot;highlighter-rouge&quot;&gt;Object.create()&lt;/code&gt;方法，会创建一个新对象，第一个参数接收一个对象，将会作为新创建对象的原型对象，第二个可选参数是属性描述符（不常用，默认是&lt;code class=&quot;highlighter-rouge&quot;&gt;undefined&lt;/code&gt;）。具体请查看&lt;a href=&quot;https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create&quot;&gt;Object.create()&lt;/a&gt;。

我们来模拟一个简易版的&lt;code class=&quot;highlighter-rouge&quot;&gt;Object.create&lt;/code&gt;：

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;createObj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;proto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;proto&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

我们平常所说的空对象，其实并不是严格意义上的空对象，它的原型对象指向&lt;code class=&quot;highlighter-rouge&quot;&gt;Object.prototype&lt;/code&gt;，还可以继承&lt;code class=&quot;highlighter-rouge&quot;&gt;hasOwnProperty&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;toString&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;valueOf&lt;/code&gt;等方法。

如果想要生成一个不继承任何属性的对象，可以使用&lt;code class=&quot;highlighter-rouge&quot;&gt;Object.create(null)&lt;/code&gt;。

如果想要生成一个平常字面量方法生成的对象，需要将其原型对象指向&lt;code class=&quot;highlighter-rouge&quot;&gt;Object.prototype&lt;/code&gt;：

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 等价于&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;3-new操作符&quot;&gt;3. &lt;code class=&quot;highlighter-rouge&quot;&gt;new&lt;/code&gt;操作符&lt;/h2&gt;

当我们使用&lt;code class=&quot;highlighter-rouge&quot;&gt;new&lt;/code&gt;时，做了些什么？

&lt;ol&gt;
  &lt;li&gt;创建一个全新对象，并将其&lt;code class=&quot;highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;属性指向构造函数的&lt;code class=&quot;highlighter-rouge&quot;&gt;prototype&lt;/code&gt;属性。&lt;/li&gt;
  &lt;li&gt;将构造函数调用的this指向这个新对象，并执行构造函数。&lt;/li&gt;
  &lt;li&gt;如果构造函数返回对象类型Object(包含Functoin, Array, Date, RegExg, Error等)，则正常返回，否则返回这个新的对象。&lt;/li&gt;
&lt;/ol&gt;

依然来模拟实现一下：

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;newOperator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'function'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'传入参数必须为函数，您传入的参数为'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// 创建一个全新对象，并将其`__proto__`属性指向构造函数的`prototype`属性&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;newObj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// 将构造函数调用的this指向这个新对象，并执行构造函数&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;newObj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// 如果构造函数返回对象类型Object，则正常返回，否则返回这个新的对象&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;newObj&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;4-function__proto__--functionprototype&quot;&gt;4. &lt;code class=&quot;highlighter-rouge&quot;&gt;Function.__proto__ === Function.prototype&lt;/code&gt;&lt;/h2&gt;

其实这里完全没必要去纠结鸡生蛋还是蛋生鸡的问题，我自己的理解是：&lt;code class=&quot;highlighter-rouge&quot;&gt;Function&lt;/code&gt;是原生构造函数，自动出现在运行环境中，所以不存在自己生成自己。之所以&lt;code class=&quot;highlighter-rouge&quot;&gt;Function.__proto__ === Function.prototype&lt;/code&gt;，是为了表明&lt;code class=&quot;highlighter-rouge&quot;&gt;Function&lt;/code&gt;作为一个原生构造函数，本身也是一个函数对象，仅此而已。

&lt;h2 id=&quot;5-真的是继承吗&quot;&gt;5. 真的是继承吗？&lt;/h2&gt;

前面我们讲到每一个对象都会从原型‘继承’属性，实际上，继承是一个十分具有迷惑性的说法，引用《你不知道的JavaScript》中的话，就是：

继承意味着复制操作，然而 JavaScript 默认并不会复制对象的属性，相反，JavaScript 只是在两个对象之间创建一个关联，这样，一个对象就可以通过委托访问另一个对象的属性，所以与其叫继承，委托的说法反而更准确些。

&lt;h1 id=&quot;十参考文章&quot;&gt;十、参考文章&lt;/h1&gt;

&lt;a href=&quot;http://www.cnblogs.com/wangfupeng1988/p/3977924.html&quot;&gt;深入理解javascript原型和闭包（完结）- 王福朋&lt;/a&gt;

&lt;a href=&quot;https://github.com/mqyqingfeng/Blog/issues/2&quot;&gt;JavaScript深入之从原型到原型链&lt;/a&gt;

&lt;a href=&quot;https://juejin.im/post/5bde7c926fb9a049f66b8b52&quot;&gt;面试官问：能否模拟实现JS的new操作符&lt;/a&gt;

&lt;a href=&quot;https://juejin.im/post/599d69fc6fb9a0248f4a7b31&quot;&gt;白话原型和原型链&lt;/a&gt;

&lt;a href=&quot;https://github.com/mqyqingfeng/Blog/issues/2&quot;&gt;JavaScript深入之从原型到原型链&lt;/a&gt;

菜鸟一枚，如果有错误或者不严谨的地方，请务必给予指正，十分感谢。如果有需要深入讨论的地方，也欢迎在评论区留下自己的见解。

&lt;strong&gt;遗留疑问：字面量方式创建对象，会不会调用Object构造函数？&lt;/strong&gt;
</description>
        <pubDate>Wed, 07 Nov 2018 11:48:05 +0000</pubDate>
        <link>https://logan70.github.io/2018/11/07/prototype/</link>
        <guid isPermaLink="true">https://logan70.github.io/2018/11/07/prototype/</guid>
        
        <category>原型</category>
        
        <category>原型链</category>
        
        
        <category>JavaSctipt</category>
        
      </item>
    
      <item>
        <title>JavaScript基础-函数节流Throttle</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#一函数为什么要节流&quot; id=&quot;markdown-toc-一函数为什么要节流&quot;&gt;一、函数为什么要节流&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;一函数为什么要节流&quot;&gt;一、函数为什么要节流&lt;/h1&gt;

有如下代码

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onmousemove&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;`第&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;次触发回调`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

当我们在PC端页面上滑动鼠标时，一秒可以可以触发约60次事件。大家也可以访问下面的在线例子进行测试。

&lt;p data-height=&quot;265&quot; data-theme-id=&quot;dark&quot; data-slug-hash=&quot;gQpxZR&quot; data-default-tab=&quot;result&quot; data-user=&quot;logan70&quot; data-pen-title=&quot;函数节流-监听鼠标移动触发次数测试&quot; class=&quot;codepen&quot;&gt;查看在线例子： &lt;a href=&quot;https://codepen.io/logan70/pen/gQpxZR/&quot;&gt;函数节流-监听鼠标移动触发次数测试&lt;/a&gt; by Logan (&lt;a href=&quot;https://codepen.io/logan70&quot;&gt;@logan70&lt;/a&gt;) on &lt;a href=&quot;https://codepen.io&quot;&gt;CodePen&lt;/a&gt;.
&lt;script async=&quot;&quot; src=&quot;https://static.codepen.io/assets/embed/ei.js&quot;&gt;&lt;/script&gt;

这里的回调函数只是打印字符串，如果回调函数更加复杂，可想而知浏览器的压力会非常大，可能降低用户体验。

&lt;code class=&quot;highlighter-rouge&quot;&gt;resize&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;scroll&lt;/code&gt;或&lt;code class=&quot;highlighter-rouge&quot;&gt;mousemove&lt;/code&gt;等事件的监听回调会被频繁触发，因此我们要对其进行限制。

</description>
        <pubDate>Fri, 02 Nov 2018 11:48:05 +0000</pubDate>
        <link>https://logan70.github.io/2018/11/02/throttle/</link>
        <guid isPermaLink="true">https://logan70.github.io/2018/11/02/throttle/</guid>
        
        <category>Throttle</category>
        
        
        <category>Javasctipt</category>
        
      </item>
    
      <item>
        <title>JavaScript基础-函数防抖Debounce</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#一函数为什么要防抖&quot; id=&quot;markdown-toc-一函数为什么要防抖&quot;&gt;一、函数为什么要防抖&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#二实现思路&quot; id=&quot;markdown-toc-二实现思路&quot;&gt;二、实现思路&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#三debounce-应用场景&quot; id=&quot;markdown-toc-三debounce-应用场景&quot;&gt;三、Debounce 应用场景&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;一函数为什么要防抖&quot;&gt;一、函数为什么要防抖&lt;/h1&gt;

有如下代码

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onresize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'触发窗口监听回调函数'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

当我们在PC上缩放浏览器窗口时，一秒可以轻松触发30次事件。手机端触发其他Dom事件监听回调时同理。

这里的回调函数只是打印字符串，如果回调函数更加复杂，可想而知浏览器的压力会非常大，用户体验会很糟糕。

&lt;code class=&quot;highlighter-rouge&quot;&gt;resize&lt;/code&gt;或&lt;code class=&quot;highlighter-rouge&quot;&gt;scroll&lt;/code&gt;等Dom事件的监听回调会被频繁触发，因此我们要对其进行限制。

&lt;h1 id=&quot;二实现思路&quot;&gt;二、实现思路&lt;/h1&gt;

函数去抖简单来说就是对于一定时间段的连续的函数调用，只让其执行一次，初步的实现思路如下：

第一次调用函数，创建一个定时器，在指定的时间间隔之后运行代码。当第二次调用该函数时，它会清除前一次的定时器并设置另一个。如果前一个定时器已经执行过了，这个操作就没有任何意义。然而，如果前一个定时器尚未执行，其实就是将其替换为一个新的定时器。目的是只有在执行函数的请求停止了一段时间之后才执行。

&lt;h1 id=&quot;三debounce-应用场景&quot;&gt;三、Debounce 应用场景&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;每次 resize/scroll 触发统计事件&lt;/li&gt;
  &lt;li&gt;文本输入的验证（连续输入文字后发送 AJAX 请求进行验证，验证一次就好）&lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Wed, 31 Oct 2018 11:48:05 +0000</pubDate>
        <link>https://logan70.github.io/2018/10/31/debounce/</link>
        <guid isPermaLink="true">https://logan70.github.io/2018/10/31/debounce/</guid>
        
        <category>Debounce</category>
        
        
        <category>Javasctipt</category>
        
      </item>
    
      <item>
        <title>Vue-Router学习笔记</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#一基础&quot; id=&quot;markdown-toc-一基础&quot;&gt;一、基础&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1-介绍&quot; id=&quot;markdown-toc-1-介绍&quot;&gt;1. 介绍&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2-基本使用&quot; id=&quot;markdown-toc-2-基本使用&quot;&gt;2. 基本使用&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#21-安装&quot; id=&quot;markdown-toc-21-安装&quot;&gt;2.1 安装&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#22-在入口文件中引入并配置&quot; id=&quot;markdown-toc-22-在入口文件中引入并配置&quot;&gt;2.2 在入口文件中引入并配置&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#23-在appvue中插入导航及视图渲染标签&quot; id=&quot;markdown-toc-23-在appvue中插入导航及视图渲染标签&quot;&gt;2.3 在&lt;code class=&quot;highlighter-rouge&quot;&gt;App.vue&lt;/code&gt;中插入导航及视图渲染标签&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;一基础&quot;&gt;一、基础&lt;/h1&gt;

&lt;h2 id=&quot;1-介绍&quot;&gt;1. 介绍&lt;/h2&gt;

Vue Router 是 Vue.js官方的&lt;strong&gt;路由管理器&lt;/strong&gt;。是我们使用Vue构建的SPA（单页应用 Single Page Application）的&lt;strong&gt;路径管理器&lt;/strong&gt;。本质上就是建立&lt;strong&gt;页面Url与组件之间的映射关系&lt;/strong&gt;。

&lt;h2 id=&quot;2-基本使用&quot;&gt;2. 基本使用&lt;/h2&gt;

&lt;h3 id=&quot;21-安装&quot;&gt;2.1 安装&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;language-cmd&quot;&gt;npm install vue-router -S
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;22-在入口文件中引入并配置&quot;&gt;2.2 在入口文件中引入并配置&lt;/h3&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 项目入口文件main.js&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 1. 引入vue、vue-router&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Vue&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'vue'&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;VueRouter&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'vue-router'&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 2. 引入项目组件&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;App&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'./App.vue'&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Home&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'./components/Home.vue'&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Hello&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'./components/Hello.vue'&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 3. 调用vue-router&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Vue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;VueRouter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 4. 创建router实例，定义路由配置&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;router&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;VueRouter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/home'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;component&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Home&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/hello'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;component&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Hello&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;// 根目录重定向至 /home&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;redirect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/home'&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 5. 创建、挂载根实例并注入路由&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Vue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;router&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;$mount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'#app'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;23-在appvue中插入导航及视图渲染标签&quot;&gt;2.3 在&lt;code class=&quot;highlighter-rouge&quot;&gt;App.vue&lt;/code&gt;中插入导航及视图渲染标签&lt;/h3&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;&amp;lt;!-- App.vue --&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;template&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;nav&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;router-link&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;to=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/home&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Home&lt;span class=&quot;nt&quot;&gt;&amp;lt;/router-link&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;router-link&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;to=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/hello&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Hello&lt;span class=&quot;nt&quot;&gt;&amp;lt;/router-link&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- 路由渲染出口 --&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;router-view&amp;gt;&amp;lt;/router-view&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</description>
        <pubDate>Mon, 22 Oct 2018 11:48:05 +0000</pubDate>
        <link>https://logan70.github.io/2018/10/22/vue-router/</link>
        <guid isPermaLink="true">https://logan70.github.io/2018/10/22/vue-router/</guid>
        
        <category>VueRouter</category>
        
        
        <category>Vue</category>
        
      </item>
    
      <item>
        <title>移动端滚动穿透问题解决方案</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#一-问题描述&quot; id=&quot;markdown-toc-一-问题描述&quot;&gt;一、 问题描述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#二-解决方案&quot; id=&quot;markdown-toc-二-解决方案&quot;&gt;二、 解决方案&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1-body-设置overflow-hidden&quot; id=&quot;markdown-toc-1-body-设置overflow-hidden&quot;&gt;1. &lt;code class=&quot;highlighter-rouge&quot;&gt;body&lt;/code&gt; 设置&lt;code class=&quot;highlighter-rouge&quot;&gt;overflow: hidden;&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;一-问题描述&quot;&gt;一、 问题描述&lt;/h1&gt;

移动端当&lt;code class=&quot;highlighter-rouge&quot;&gt;position:fixed&lt;/code&gt;的弹窗弹出时，滑动弹窗，下层的页面会跟随滚动，这就叫做&lt;strong&gt;滚动穿透&lt;/strong&gt;

&lt;h1 id=&quot;二-解决方案&quot;&gt;二、 解决方案&lt;/h1&gt;

&lt;h2 id=&quot;1-body-设置overflow-hidden&quot;&gt;1. &lt;code class=&quot;highlighter-rouge&quot;&gt;body&lt;/code&gt; 设置&lt;code class=&quot;highlighter-rouge&quot;&gt;overflow: hidden;&lt;/code&gt;&lt;/h2&gt;

当弹窗弹出时，设置&lt;code class=&quot;highlighter-rouge&quot;&gt;body&lt;/code&gt;元素的&lt;code class=&quot;highlighter-rouge&quot;&gt;overflow&lt;/code&gt;属性为&lt;code class=&quot;highlighter-rouge&quot;&gt;hidden&lt;/code&gt;，暴力去除滚动。

&lt;strong&gt;缺点&lt;/strong&gt;：会丢失页面的滚动位置，需要使用js进行还原

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;CSS&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.modal-open&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;overflow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;100%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// JS&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;modalManager&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;scrollTop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;getScrollTop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;scrollTop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;documentElement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;scrollTop&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;scrollTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;scrollTop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;documentElement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;scrollTop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;position&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ele&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;scrollTop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getScrollTop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;querySelector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ele&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;display&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'block'&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;classList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'modal-open'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;scrollTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;scrollTop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;hide&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ele&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;querySelector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ele&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;display&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'none'&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;classList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'modal-open'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;scrollTop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</description>
        <pubDate>Fri, 19 Oct 2018 11:48:05 +0000</pubDate>
        <link>https://logan70.github.io/2018/10/19/modal-scrolling/</link>
        <guid isPermaLink="true">https://logan70.github.io/2018/10/19/modal-scrolling/</guid>
        
        <category>滚动穿透</category>
        
        
        <category>Mobile</category>
        
      </item>
    
      <item>
        <title>Node.js入门-7-数据库MongoDB</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#一-数据库&quot; id=&quot;markdown-toc-一-数据库&quot;&gt;一、 数据库&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1-基本概念&quot; id=&quot;markdown-toc-1-基本概念&quot;&gt;1. 基本概念&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;一-数据库&quot;&gt;一、 数据库&lt;/h1&gt;

&lt;h2 id=&quot;1-基本概念&quot;&gt;1. 基本概念&lt;/h2&gt;

数据库就是按照一定的数据结构来组织、存储和管理数据的仓库

&lt;blockquote&gt;
  我们写的程序都是在内存中运行的，一旦程序运行结束或者计算机断电，内存中的数据都会全部丢失。所以我们就需要将一些程序运行的数据持久化到硬盘之中，以确保数据的安全性。
&lt;/blockquote&gt;

数据库是大批量数据持久化的普遍选择。&lt;strong&gt;优点&lt;/strong&gt;如下：

&lt;ul&gt;
  &lt;li&gt;1、数据库是有结构的，数据与数据之间可以建立各种关系，类似网状拓扑图。&lt;/li&gt;
  &lt;li&gt;2、数据库能够提供各种接口，让数据的处理（增删改查操作）变得快捷简单。&lt;/li&gt;
  &lt;li&gt;3、给各种语言（PHP、jsp、.net等）提供了完善的接口&lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Thu, 26 Jul 2018 11:48:05 +0000</pubDate>
        <link>https://logan70.github.io/2018/07/26/node-7-mongodb/</link>
        <guid isPermaLink="true">https://logan70.github.io/2018/07/26/node-7-mongodb/</guid>
        
        <category>MongoDB</category>
        
        
        <category>Node</category>
        
      </item>
    
      <item>
        <title>Node.js入门-6-stream(流)</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#stream流&quot; id=&quot;markdown-toc-stream流&quot;&gt;Stream（流）&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;stream流&quot;&gt;Stream（流）&lt;/h1&gt;

流（stream）是一种在 Node.js 中处理流式数据的抽象接口。流可以是可读的、可写的、或是可读写的。

Node.js 中有四种基本的流类型：

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Writable&lt;/code&gt; :  可写入数据的流（例如 &lt;code class=&quot;highlighter-rouge&quot;&gt;fs.createWriteStream()&lt;/code&gt;）。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Readable&lt;/code&gt; :  可读取数据的流（例如 &lt;code class=&quot;highlighter-rouge&quot;&gt;fs.createReadStream()&lt;/code&gt;）。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Duplex&lt;/code&gt; :  可读又可写的流（例如 &lt;code class=&quot;highlighter-rouge&quot;&gt;net.Socket&lt;/code&gt;）。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Transform&lt;/code&gt; :  在读写过程中可以修改或转换数据的 Duplex 流（例如 &lt;code class=&quot;highlighter-rouge&quot;&gt;zlib.createDeflate()&lt;/code&gt;）。&lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Fri, 20 Jul 2018 11:48:05 +0000</pubDate>
        <link>https://logan70.github.io/2018/07/20/node-6-stream/</link>
        <guid isPermaLink="true">https://logan70.github.io/2018/07/20/node-6-stream/</guid>
        
        <category>Stream</category>
        
        
        <category>Node</category>
        
      </item>
    
      <item>
        <title>Node.js入门-5-FileSystem(文件系统)</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#一概述&quot; id=&quot;markdown-toc-一概述&quot;&gt;一、概述&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;一概述&quot;&gt;一、概述&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;在Node中，与文件系统的交互是非常重要的，服务器的本质就是将本地的文件发送给远程的客户端&lt;/li&gt;
  &lt;li&gt;Node通过fs模块来和文件系统进行交互，该模块提供了一组类似UNIX（POSIX）标准的文件操作API来打开、读取、写入文件，以及与其交互&lt;/li&gt;
  &lt;li&gt;要使用fs模块，首先要从核心模块中加载：&lt;code class=&quot;highlighter-rouge&quot;&gt;const fs = require('fs')&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Node.js 文件系统（fs模块) 模块中的方法均有异步和同步版本。&lt;/li&gt;
  &lt;li&gt;异步的方法函数最后一个参数为回调函数，回调函数的第一个参数包含了错误信息（error）&lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Sat, 14 Jul 2018 11:48:05 +0000</pubDate>
        <link>https://logan70.github.io/2018/07/14/node-5-fs/</link>
        <guid isPermaLink="true">https://logan70.github.io/2018/07/14/node-5-fs/</guid>
        
        <category>FileSystem</category>
        
        
        <category>Node</category>
        
      </item>
    
      <item>
        <title>Node.js入门-4-Buffer(缓冲器)</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#一概述&quot; id=&quot;markdown-toc-一概述&quot;&gt;一、概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#二创建-buffer-的方法&quot; id=&quot;markdown-toc-二创建-buffer-的方法&quot;&gt;二、创建 Buffer 的方法&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;一概述&quot;&gt;一、概述&lt;/h1&gt;

在 ES6 引入 &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray&quot;&gt;TypedArray&lt;/a&gt; 之前，JavaScript 语言没有读取或操作二进制数据流的机制。 Buffer 类被引入作为 NodeJS API 的一部分，使其可以在 TCP 流或文件系统操作等场景中处理二进制数据流。Buffer 属于 Global 对象，使用时不需引入，且 Buffer 的大小在创建时确定，无法调整。

&lt;strong&gt;注意：&lt;/strong&gt;

&lt;ul&gt;
  &lt;li&gt;Buffer的结构和数组很像，操作的方法也和数组类似&lt;/li&gt;
  &lt;li&gt;Buffer中是以二进制的方式存储数据的&lt;/li&gt;
  &lt;li&gt;Buffer是Node自带的，不需要引入，直接使用即可&lt;/li&gt;
  &lt;li&gt;Buffer 一旦被创建，长度将保持不变。&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;二创建-buffer-的方法&quot;&gt;二、创建 Buffer 的方法&lt;/h1&gt;

在v6.0之前创建Buffer对象直接使用&lt;code class=&quot;highlighter-rouge&quot;&gt;new Buffer()&lt;/code&gt;构造函数来创建对象实例，但是Buffer对内存的权限操作相比很大，可以直接捕获一些敏感信息，所以在v6.0以后，官方文档里面建议使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;Buffer.from()&lt;/code&gt; 接口去创建Buffer对象。

</description>
        <pubDate>Wed, 04 Jul 2018 11:48:05 +0000</pubDate>
        <link>https://logan70.github.io/2018/07/04/node-4-buffer/</link>
        <guid isPermaLink="true">https://logan70.github.io/2018/07/04/node-4-buffer/</guid>
        
        <category>Buffer</category>
        
        
        <category>Node</category>
        
      </item>
    
      <item>
        <title>Node.js入门-3-基础</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#一命令行窗口常用命令&quot; id=&quot;markdown-toc-一命令行窗口常用命令&quot;&gt;一、命令行窗口常用命令&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#二进程和线程&quot; id=&quot;markdown-toc-二进程和线程&quot;&gt;二、进程和线程&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1-进程&quot; id=&quot;markdown-toc-1-进程&quot;&gt;1. 进程&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2-线程&quot; id=&quot;markdown-toc-2-线程&quot;&gt;2. 线程&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;一命令行窗口常用命令&quot;&gt;一、命令行窗口常用命令&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;dir&lt;/code&gt; ：列出当前目录下所有文件及文件夹&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;cd dName&lt;/code&gt; ：进入指定目录，&lt;code class=&quot;highlighter-rouge&quot;&gt;cd ..&lt;/code&gt;为返回上级目录&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;md&lt;/code&gt;或者&lt;code class=&quot;highlighter-rouge&quot;&gt;mkdir&lt;/code&gt; ：创建文件夹&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;rd&lt;/code&gt;或者&lt;code class=&quot;highlighter-rouge&quot;&gt;rmdir&lt;/code&gt; ：删除文件夹&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;rd&lt;/code&gt;或者&lt;code class=&quot;highlighter-rouge&quot;&gt;rmdir&lt;/code&gt; ：删除文件夹&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;cls&lt;/code&gt; : 清屏&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;exit&lt;/code&gt; : 退出命令行&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;二进程和线程&quot;&gt;二、进程和线程&lt;/h1&gt;

&lt;h2 id=&quot;1-进程&quot;&gt;1. 进程&lt;/h2&gt;

&lt;strong&gt;概念&lt;/strong&gt;

进程是系统进行资源分配和调度的基本单位，是操作系统结构的基础，进程为程序的运行提供了必备的环境。

&lt;blockquote&gt;
  我们写的代码和程序都是放在进程里面的，相当于工厂中的车间
&lt;/blockquote&gt;

&lt;h2 id=&quot;2-线程&quot;&gt;2. 线程&lt;/h2&gt;

&lt;strong&gt;概念&lt;/strong&gt;

线程是计算机中的最小计算单位，负责执行进程中的程序

&lt;blockquote&gt;
  相当于车间中的操作工人
&lt;/blockquote&gt;

</description>
        <pubDate>Wed, 20 Jun 2018 11:48:05 +0000</pubDate>
        <link>https://logan70.github.io/2018/06/20/node-3/</link>
        <guid isPermaLink="true">https://logan70.github.io/2018/06/20/node-3/</guid>
        
        <category>Node.js</category>
        
        
        <category>Node</category>
        
      </item>
    
  </channel>
</rss>

