<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Wisdom</title>
    <description></description>
    <link>https://findwisdom.github.io//</link>
    <atom:link href="https://findwisdom.github.io//feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Fri, 10 Jul 2020 11:08:15 +0000</pubDate>
    <lastBuildDate>Fri, 10 Jul 2020 11:08:15 +0000</lastBuildDate>
    <generator>Jekyll v3.8.7</generator>
    
      <item>
        <title>CSS BEM 书写规范</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;#二-bem解决的问题&quot; id=&quot;markdown-toc-二-bem解决的问题&quot;&gt;(二) BEM解决的问题&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;/ul&gt;

&lt;p&gt;BEM是一种让你可以快速开发网站并对此进行多年维护的技术。&lt;/p&gt;

&lt;h3 id=&quot;一-简介&quot;&gt;(一) 简介&lt;/h3&gt;

&lt;p&gt;一开始，Yandex公司推出的BEM，包括了规范以及其配套构建工具。&lt;/p&gt;

&lt;p&gt;如今提到的BEM主要是指其中的规范，在&lt;a href=&quot;http://getbem.com&quot;&gt;BEM最新的推广页&lt;/a&gt;B中，对其的描述为：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;BEM是一种命名方法，能够帮助你在前端开发中实现可复用的组件和代码共享&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;二-bem解决的问题&quot;&gt;(二) BEM解决的问题&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;css的样式应用是全局性的，没有作用域可言。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;考虑以下场景：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;场景一：开发一个弹窗组件，在现有页面中测试都没问题，一段时间后，新需求新页面，该页面一打开这个弹窗组件，页面中样式都变样了，一查问题，原来是弹窗组件和该页面的样式相互覆盖了，接下来就是修改覆盖样式的选择器…又一段时间，又开发新页面，每次为元素命名都心惊胆战，求神拜佛，没写一条样式，F5都按多几次，每个组件都测试一遍…&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;场景二：承接上文，由于页面和弹窗样式冲突了，所以把页面的冲突样式的选择器加上一些结构逻辑，比如子选择器、标签选择器，借此让选择器独一无二。一段时间后，新同事接手跟进需求，对样式进行修改，由于选择器是一连串的结构逻辑，看不过来，嫌麻烦，就干脆在样式文件最后用另一套选择器，加上了覆盖样式…接下来又有新的需求…最后的结果，一个元素对应多套样式，遍布整个样式文件…&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;以往开发组件，我们都用“重名概率小”或者干脆起个“当时认为是独一无二的名字”来保证样式不冲突，这是不可靠的。
理想的状态下，我们开发一套组件的过程中，我们应该可以随意的为其中元素进行命名，而不必担心它是否与组件以外的样式发生冲突。&lt;/p&gt;

&lt;p&gt;BEM解决这一问题的思路在于，由于项目开发中，每个组件都是唯一无二的，其名字也是独一无二的，组件内部元素的名字都加上组件名，并用元素的名字作为选择器，自然组件内的样式就不会与组件外的样式冲突了。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;这是通过组件名的唯一性来保证选择器的唯一性，从而保证样式不会污染到组件外。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;BEM的意思就是块（block）、元素（element）、修饰符（modifier）,&lt;/p&gt;

&lt;p&gt;是由Yandex团队提出的一种前端命名方法论。&lt;/p&gt;

&lt;p&gt;这种巧妙的命名方法让你的CSS类对其他开发者来说更加透明而且更有意义。B&lt;/p&gt;

&lt;p&gt;EM命名约定更加严格，而且包含更多的信息，它们用于一个团队开发一个耗时的大项目。&lt;/p&gt;

&lt;p&gt;命名约定的模式如下：&lt;/p&gt;

&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;nc&quot;&gt;.block&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.block__element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;.block--modifier&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;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.block&lt;/code&gt; 代表了更高级别的抽象或组件。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.block__element&lt;/code&gt; 代表.block的后代，用于形成一个完整的.block的整体。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.block--modifier&lt;/code&gt; 代表.block的不同状态或不同版本。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;其中，block表示的是独立的分块或组件；element表示每个block中更细粒度的元素；modifier则通常会用来表示该block或者element不同的类型和状态。&lt;/p&gt;

&lt;p&gt;举个例子，例如我们有一个列表&lt;/p&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;nt&quot;&gt;&amp;lt;ul&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list&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;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;item&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;learn html&lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;item underline&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;learn css&lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;item&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;learn js&lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;列表容器的class为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.list&lt;/code&gt;，列表内每条记录的class为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.item&lt;/code&gt;，其中，还为第二个条记录添加了一个下划线&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.underline&lt;/code&gt;。简单的css如下&lt;/p&gt;

&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;nc&quot;&gt;.list&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;15px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&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;nc&quot;&gt;.list&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.item&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;#333&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;15px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;#333&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;list-style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;none&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;nc&quot;&gt;.list&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.underline&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;#111&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;text-decoration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;underline&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;p&gt;这样的命名方式，我们在阅读html时并不能迅速了解：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.item&lt;/code&gt;是只能在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.list&lt;/code&gt;中使用么，&lt;/p&gt;

&lt;p&gt;它是仅仅定义在这个组件内的一部分么？&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.underline&lt;/code&gt;是一个通用样式么，&lt;/p&gt;

&lt;p&gt;我想修改列表的中underline的记录为红色，这会影响到项目其他地方么？&lt;/p&gt;

&lt;p&gt;这时候，我们就可以使用BEM方式来命名我们的class&lt;/p&gt;

&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;nc&quot;&gt;.list&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;15px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&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;nc&quot;&gt;.list__item&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10px&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3px&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;#333&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;15px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;#333&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;list-style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;none&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;nc&quot;&gt;.list__item--underline&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;#111&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;text-decoration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;underline&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-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;nt&quot;&gt;&amp;lt;ul&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list&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;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list__item&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;learn html&lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list__item list__item--underline&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;learn css&lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list__item&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;learn js&lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这段代码的一大优势就是增加了它的自解释性：一定程度上，它的class名本身就是一个简易的文档。&lt;/p&gt;

&lt;p&gt;这里还需要避免一个误区，BEM命名规范里，我们的CSS并不会关心HTML中dom元素的层级结构。&lt;/p&gt;

&lt;p&gt;它的核心着眼点还是我们定义的块（block）、元素（element）、修饰符（modifier）这三部分。&lt;/p&gt;

&lt;p&gt;因为关注点不同，所以一个block内的所有element，在CSS中并不会考虑层级，&lt;/p&gt;

&lt;p&gt;因此也就没有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.list__item__avatar&lt;/code&gt;这种写法&lt;/p&gt;

&lt;p&gt;而是把这个img也看作block中的元素&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.list__avatar&lt;/code&gt;&lt;/p&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;nt&quot;&gt;&amp;lt;ul&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list&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;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list__item&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    ![](avatar.png)
    learn html
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list__item list__item--underline&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;learn css&lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list__item&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;learn js&lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;从这个例子看一看出，CSS部分并不关心dom层级结构，而是在block下面有哪些element，这些element又有哪些modifier。&lt;/p&gt;

&lt;p&gt;基于这个思想，我们可以知道，如果一个block里面含有其他block并不会违反BEM的原则。&lt;/p&gt;

&lt;p&gt;例如上面这个列表的例子，其中头像avatar原本只是一个简单的element，&lt;/p&gt;

&lt;p&gt;现在如果变成了一个很复杂的组件——包括图片、姓名和标签，那么可能会有这么一个block&lt;/p&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;nt&quot;&gt;&amp;lt;ul&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list&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;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list__item&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;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list__avatar&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;img&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list__head list__head--female&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;span&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list__name&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;span&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list__tag&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    learn html
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list__item list__item--underline&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;learn css&lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list__item&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;learn js&lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;我们可以为avatar创建一个新的block&lt;/p&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;nt&quot;&gt;&amp;lt;ul&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list&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;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list__item&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;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;avatar&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;img&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;avatar__head avatar__head--female&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;span&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;avatar__name&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;span&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;avatar__tag&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    learn html
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list__item list__item--underline&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;learn css&lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;li&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list__item&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;learn js&lt;span class=&quot;nt&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;那么你可能会有疑问，什么时候需要在将一个elment重新抽象为新的block呢？&lt;/p&gt;

&lt;p&gt;仅仅当我们的dom元素变得很多的时候么？&lt;/p&gt;

&lt;p&gt;其实，BEM中的block一定程度上可以理解为一个“独立的块”。&lt;/p&gt;

&lt;p&gt;独立就意味着，把这一部分放到其他部分也可以正常展示与使用，它不会依赖其父元素或兄弟元素。&lt;/p&gt;

&lt;p&gt;而在另一个维度上面来说，也就是视觉设计的维度，当UI设计师给出UI稿后，其中的一些设计元素或组件会重复出现，这些部分也是可以考虑的。&lt;/p&gt;

&lt;p&gt;所以理解UI设计稿并不是指简单的还原，其中的设计原则与规范也值得揣摩。&lt;/p&gt;

&lt;p&gt;从上面的简单介绍可以看出，BEM有着一些优点:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;class的单一职责原则、开闭原则&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;模块化思想，一般来说遵循这个方法的组件可以迁移环境&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;一定程度上，避免命名的污染&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;自解释性。可以直观看出各个class之间的依赖关系以及它们的作用范围（.list__item和.list__item–underline都是依赖于.list的，因此它们不能脱离于.list存在）&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;当然，BEM仅仅是一种命名规范或建议。在没有约束的情况下，你随时都可以违反。&lt;/p&gt;

&lt;p&gt;所以我们可以借助类似BEM-constructor的工具，既帮我们进行一定的约束，同时也省去一些繁琐的重复工作。&lt;/p&gt;

&lt;h3 id=&quot;三-总结&quot;&gt;(三) 总结&lt;/h3&gt;

&lt;p&gt;BEM是一种命名规范，或者说是一种使用建议。&lt;/p&gt;

&lt;p&gt;他的目的是帮助我们写出更易维护与协作的代码，更多的是在代码规范的层面上帮助我们解决CSS模块化中的问题。&lt;/p&gt;

&lt;p&gt;然而，也不得不承认，它距离我们梦想中的CSS模块化还有这很长的距离。&lt;/p&gt;

&lt;p&gt;但是无论如何，其中蕴含的一些组件化与CSS结构组织方式的想法也是值得我们去思考的。&lt;/p&gt;

</description>
        <pubDate>Fri, 10 Jul 2020 10:46:05 +0000</pubDate>
        <link>https://findwisdom.github.io//2020/07/10/css-bem/</link>
        <guid isPermaLink="true">https://findwisdom.github.io//2020/07/10/css-bem/</guid>
        
        <category>css</category>
        
        
        <category>规范</category>
        
      </item>
    
      <item>
        <title>Markdown语法：mddir工具自动生成文件目录结构</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#一-安装mddir工具&quot; id=&quot;markdown-toc-一-安装mddir工具&quot;&gt;(一) 安装mddir工具&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#二-使用mddir工具&quot; id=&quot;markdown-toc-二-使用mddir工具&quot;&gt;(二) 使用mddir工具&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;mddir是个能帮助我们快速生成markdown语法形式的文件目录结构的工具，详细说明请参考官方文档&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.npmjs.com/package/mddir&quot;&gt;官方文档&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;一-安装mddir工具&quot;&gt;(一) 安装mddir工具&lt;/h3&gt;

&lt;p&gt;要安装mddir工具，必须先安装npm包管理工具。我的npm包管理工具是在安装node时自动安装的&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    npm install mddir -g
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;二-使用mddir工具&quot;&gt;(二) 使用mddir工具&lt;/h3&gt;

&lt;p&gt;利用命令行进入对应文件夹，然后输入并执行mddir命令。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;     mddir
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;最终在对应文件夹内会生成一个directoryList.md文件：&lt;/p&gt;

&lt;p&gt;第一行出现的’undefined’用父文件夹名，输入代替即可。&lt;/p&gt;

&lt;p&gt;注意，上图内容还是纯文本内容，markdown解析器解析渲染之后并不会正确显示，&lt;/p&gt;

&lt;p&gt;而是需要像markdown中的代码块一样用两个三重反引号(```)包围起来，最终结果如下所示：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
|-- 对应文件夹
    |-- .browserslistrc
    |-- .editorconfig
    |-- .env.cloudDev
    |-- .env.cloudMine
    |-- .env.cloudPro
    |-- .env.cloudTest
    |-- .env.private.development
    |-- .env.private.production
    |-- .env.private.test
    |-- .env.public.development
    |-- .env.public.production
    |-- .env.public.test
    |-- .eslintrc.js
    |-- .gitignore
    |-- .npmignore
    |-- .npmrc
    |-- .prettierrc.js
    |-- babel.config.js
    |-- package-lock.json
    |-- package.json
    |-- plopfile.js
    |-- postcss.config.js
    |-- README.md
    |-- vue.config.js
    |-- .idea
    |   |-- $PRODUCT_WORKSPACE_FILE$
    |   |-- misc.xml
    |   |-- modules.xml
    |   |-- scaffold-ui.iml
    |   |-- vcs.xml
    |   |-- workspace.xml
    |   |-- inspectionProfiles
    |       |-- Project_Default.xml
    |-- docs
    |   |-- css-module.md
    |   |-- scaffold.md
    |-- plop-templates
    |   |-- view
    |       |-- dialog.hbs
    |       |-- index.hbs
    |       |-- service.hbs
    |       |-- validations.hbs
    |-- public
    |   |-- favicon.ico
    |   |-- index.html
    |-- src
        |-- App.vue
        |-- main.js
        |-- permission.js
        |-- assets
        |   |-- logo.png
        |   |-- 404_images
        |   |   |-- 404.png
        |   |   |-- 404_cloud.png
        |   |-- svg
        |   |   |-- icon-wring.svg
        |   |   |-- logo.svg
        |   |   |-- nav-home.svg
        |   |   |-- nav-point.svg
        |   |   |-- nav-theme.svg
        |   |   |-- nav-usage.svg
        |   |   |-- park.svg
        |   |-- usage
        |       |-- carbon-css-module.png
        |       |-- carbon-scaffold.png
        |-- components
        |   |-- SvgIcon.vue
        |   |-- app
        |   |   |-- AppBreadcrumb.vue
        |   |   |-- AppDialog.vue
        |   |   |-- AppFooter.vue
        |   |   |-- AppHeader.vue
        |   |   |-- AppAside
        |   |       |-- index.vue
        |   |       |-- Link.vue
        |   |       |-- SidebarItem.vue
        |   |-- dialog
        |   |   |-- plop.vue
        |   |-- layout
        |   |   |-- AppLayout.vue
        |   |   |-- ContainerLayout.vue
        |   |-- table
        |       |-- TableDelete.vue
        |       |-- TableFooter.vue
        |       |-- TableHeader.vue
        |-- plugins
        |   |-- common-register
        |   |   |-- index.js
        |   |-- css-module
        |   |   |-- class-context.js
        |   |   |-- class-props.js
        |   |   |-- index.js
        |   |   |-- utils.js
        |   |-- isyscore-ui
        |   |   |-- index.js
        |   |-- vue-life
        |       |-- index.js
        |-- router
        |   |-- index.js
        |   |-- routerRoles.js
        |   |-- routes.js
        |-- services
        |   |-- plop.js
        |   |-- todo.js
        |   |-- user.js
        |-- store
        |   |-- getters.js
        |   |-- index.js
        |   |-- mutations.js
        |   |-- state.js
        |   |-- types.js
        |-- styles
        |   |-- color.css
        |   |-- color.scss
        |   |-- flex.css
        |   |-- flex.scss
        |   |-- offset.css
        |   |-- offset.scss
        |   |-- reset.css
        |   |-- reset.scss
        |   |-- text.css
        |   |-- text.scss
        |   |-- _variables.scss
        |-- utils
        |   |-- baseValidate.js
        |   |-- loading.js
        |   |-- message.js
        |   |-- number.js
        |   |-- object.js
        |   |-- querystring.js
        |   |-- service.js
        |   |-- throttle.js
        |   |-- typeis.js
        |   |-- validators.js
        |-- validations
        |   |-- plop.js
        |-- views
            |-- 404.vue
            |-- Home.vue
            |-- Login.vue
            |-- plop.vue
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</description>
        <pubDate>Wed, 08 Jan 2020 10:46:05 +0000</pubDate>
        <link>https://findwisdom.github.io//2020/01/08/markdown-mddir/</link>
        <guid isPermaLink="true">https://findwisdom.github.io//2020/01/08/markdown-mddir/</guid>
        
        <category>mddir</category>
        
        
        <category>工具</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;#三-知乎总结&quot; id=&quot;markdown-toc-三-知乎总结&quot;&gt;(三) 知乎总结&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;从沪归渝，心有戚戚，停止增长，是自身最大的恐惧，希望能从前人的经验里。寻求安宁的方法。&lt;/p&gt;

&lt;p&gt;人们关于智力的说法很多是观点鲜明的，有的人认为是由基因决定的，有的人认为是由后天努力训练得到的。&lt;/p&gt;

&lt;p&gt;我个人认为这二者皆有之，天赋的发挥需要环境的土壤。&lt;/p&gt;

&lt;p&gt;可以认为大多数人的基因其实是相差不大的就像大家的饭量，相差不会太大。&lt;/p&gt;

&lt;p&gt;成功学之所以为忽悠人的一个原因，是因为其零碎的观点。不能更精确的应用到个体的实际情况。&lt;/p&gt;

&lt;p&gt;我们需要有一些科学的方法来学习这一点。&lt;/p&gt;

&lt;h3 id=&quot;一-思维模式&quot;&gt;(一) 思维模式&lt;/h3&gt;

&lt;p&gt;终生成长一书中将人的思维模式分为两种一种为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;固定思维模式&lt;/code&gt;一种为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;成长思维模式&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;固定思维模式更专注于结果为导向，认为结果是衡量一个人的标准，恐惧失败而不愿承担挑战，不能正确的衡量自己。&lt;/p&gt;

&lt;p&gt;成长思维模式更关注于过程，更加愿意接受挑战，于历练之中认识自己，关注于成长的办法。&lt;/p&gt;

&lt;h3 id=&quot;二-思维模式解析&quot;&gt;(二) 思维模式解析&lt;/h3&gt;

&lt;p&gt;固定思维倾向于稳定安全，顺从于本能恰好符合于人性。 自我存在的认知同改变环境的能力是我们与生俱来的的能力。&lt;/p&gt;

&lt;p&gt;事物是永远改变的，成长型思维有助于在告诉变革的环境中生存，人的资源不同为达结果需要消耗的资源也不对等（时间资源，社会资源）。&lt;/p&gt;

&lt;p&gt;太过关注结果，无能为力之感的确让人抑郁， 加倍努力对固定思维者来说是风险，不够努力对成长型思维是风险。&lt;/p&gt;

&lt;p&gt;然而对现状有所不满的话， 也许成长型思维更适合我们&lt;/p&gt;

&lt;h4 id=&quot;三-知乎总结&quot;&gt;(三) 知乎总结&lt;/h4&gt;

&lt;p&gt;读完后我是懵的，只知道讲了两种思维模式，其他的什么都没留下来。&lt;/p&gt;

&lt;p&gt;于是有了第二遍、第三遍，当读完第三遍我幡然醒悟，原来成长型思维模式与固定型思维模式直接决定了我们的价值观与行为方式进而决定了不同的人生。&lt;/p&gt;

&lt;p&gt;所谓成长型思维模式是指不相信天才，认为才能是可以通过后天努力提升的。&lt;/p&gt;

&lt;p&gt;因此成长型思维模式者愿意迎接挑战，他们认为失败不过是一次经验的积累而矣。&lt;/p&gt;

&lt;p&gt;相反的，固定型思维模式者则认为才能是固定不变的，无法通过后天提升。&lt;/p&gt;

&lt;p&gt;因此他们拒绝努力，认为努力是无能者的行为，失败在固定型思维模式者眼中不是一个行为而是对一个人定性的评价，一次失败永远失败。&lt;/p&gt;

</description>
        <pubDate>Tue, 07 Jan 2020 10:46:05 +0000</pubDate>
        <link>https://findwisdom.github.io//2020/01/07/Lifelong-growth/</link>
        <guid isPermaLink="true">https://findwisdom.github.io//2020/01/07/Lifelong-growth/</guid>
        
        <category>学习笔记</category>
        
        <category>心理学</category>
        
        
        <category>心理学</category>
        
      </item>
    
      <item>
        <title>SMART原则</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#一-smart原则五式&quot; id=&quot;markdown-toc-一-smart原则五式&quot;&gt;(一) SMART原则五式&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1specific具体的&quot; id=&quot;markdown-toc-1specific具体的&quot;&gt;1.Specific（具体的）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2measurable可衡量的&quot; id=&quot;markdown-toc-2measurable可衡量的&quot;&gt;2.Measurable（可衡量的）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#3attainable可实现的&quot; id=&quot;markdown-toc-3attainable可实现的&quot;&gt;3.Attainable（可实现的）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#4relevant相关的&quot; id=&quot;markdown-toc-4relevant相关的&quot;&gt;4.Relevant（相关的）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#5time-based有时间限制的&quot; id=&quot;markdown-toc-5time-based有时间限制的&quot;&gt;5.Time-Based（有时间限制的）&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;

&lt;p&gt;什么叫 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SMART原则&lt;/code&gt;？SMART原则，源自美国马里兰大学管理学及心理学教授洛克。&lt;/p&gt;

&lt;p&gt;S-M-A-R-T这5个字母，代表Specific（具体的），Measurable（可衡量的），Attainable（可实现的），Relevant（相关的），和Time-Based（有时间限制的）。&lt;/p&gt;

&lt;h3 id=&quot;一-smart原则五式&quot;&gt;(一) SMART原则五式&lt;/h3&gt;

&lt;p&gt;今天，我们以KR1为例子就来拆解一下”SMART原则”的五个招式。&lt;/p&gt;

&lt;h4 id=&quot;1specific具体的&quot;&gt;1.Specific（具体的）&lt;/h4&gt;

&lt;p&gt;具体的，就是一刀砍掉模棱两可。&lt;/p&gt;

&lt;p&gt;比如’KR1：持续提高产品质量’，什么叫产品质量？没有具体的界定，就无法评判、衡量与执行。&lt;/p&gt;

&lt;p&gt;你可以试着把它修改为：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;消灭致命的产品缺陷数；&lt;/li&gt;
  &lt;li&gt;降低严重的产品缺陷数；&lt;/li&gt;
  &lt;li&gt;提高应用商店中APP的评分。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;当然这些大项还可以拆分为具体小项&lt;/p&gt;

&lt;p&gt;且划清其相应比重，这就变得非常具体了。&lt;/p&gt;

&lt;h4 id=&quot;2measurable可衡量的&quot;&gt;2.Measurable（可衡量的）&lt;/h4&gt;

&lt;p&gt;可衡量的，就是一刀砍掉标准争议。&lt;/p&gt;

&lt;p&gt;以KR来说什么才算0.9分，什么只有0.3分。&lt;/p&gt;

&lt;p&gt;降低多少严重的产品缺陷数，算是降低？提高多少质量评分，算是提高？&lt;/p&gt;

&lt;p&gt;我们进一步修改这条KR为：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;致命产品的缺陷数保持为零；&lt;/li&gt;
  &lt;li&gt;严重的产品缺陷数减少50%；&lt;/li&gt;
  &lt;li&gt;应用商店中APP的评分从4.1分提升到5分。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这样，你和研发总监就不会争论。&lt;/p&gt;

&lt;h4 id=&quot;3attainable可实现的&quot;&gt;3.Attainable（可实现的）&lt;/h4&gt;

&lt;p&gt;可实现的，就是一刀砍掉不切实际。应用商店中APP的评分，从4.1分提升到5分，这可能吗。&lt;/p&gt;

&lt;p&gt;你看了一下应用商店排名前100个APP，发现没有一个能到达满分5分。这是一个不可实现的目标。&lt;/p&gt;

&lt;p&gt;我们还要接着修改，这条KR可能应该是这样：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;致命的产品缺陷数保持为零；&lt;/li&gt;
  &lt;li&gt;严重的产品缺陷数减少50%；&lt;/li&gt;
  &lt;li&gt;应用商店中APP的评分从4.1分提升到4.5分。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;跳一跳，够得着，团队才会有斗志。&lt;/p&gt;

&lt;h4 id=&quot;4relevant相关的&quot;&gt;4.Relevant（相关的）&lt;/h4&gt;

&lt;p&gt;相关的意思，就是一刀砍掉无关目标。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;应用商店中APP的评分，从4.1分提升到4.5分，真的和产品质量有关吗？&lt;/li&gt;
  &lt;li&gt;分数提升，会不会是新功能导致的呢？或者是因为用户太喜欢新界面了？&lt;/li&gt;
  &lt;li&gt;应用评分和产品质量有相关性，但还不足够强。我们可以进一步改为：致命的产品缺陷数保持为零；严重产品缺陷数减少50%；应用商店中APP的差评中，汇报产品缺陷的比率减少50%。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这样，质量问题，就不会被设计问题所掩盖。&lt;/p&gt;

&lt;h4 id=&quot;5time-based有时间限制的&quot;&gt;5.Time-Based（有时间限制的）&lt;/h4&gt;

&lt;p&gt;有时间限制的意思，就是一刀砍掉无限拖延。&lt;/p&gt;

&lt;p&gt;产品缺陷数减少50%，很好；差评中汇报缺陷的比率减少50%，也很好。&lt;/p&gt;

&lt;p&gt;但是，多长时间呢？1年之后实现，还有意义吗？如果没有时间限制，这个目标设置就无意义。&lt;/p&gt;

&lt;p&gt;于是，我们接着修改这条KR，加上时间限制：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;致命的产品缺陷数保持为零；&lt;/li&gt;
  &lt;li&gt;严重产品缺陷数在3个月内减少50%；
*应用商店中APP的差评中，汇报产品缺陷的比率;
*在3个月内减少50%。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;至此，我们终于手持&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OKR&lt;/code&gt;这把宝刀，挥舞&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SMART原则&lt;/code&gt;这套刀法。&lt;/p&gt;

&lt;p&gt;把”持续提高产品质量”这个千面哈姆雷特，变为一个真正目标一致的KR。&lt;/p&gt;

&lt;p&gt;你和你的研发总监再次坐在一起，就不会再为”啊？你居然是那么理解的？”
“啊？这也算是完成了啊？”而消耗内部资源了。”&lt;/p&gt;

&lt;h3 id=&quot;二-思考应用&quot;&gt;(二) 思考应用&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;比不定目标更可怕的，是不管遇到什么情况都完全按照最初的目标去执行。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;就像尽信书不如无书一样，目标也不是一个固定不变的东西，要根据具体的情况去判断和分析，进而决定是要继续坚持还是及时更改。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;OKR当中的O的确可以定一些不那么具体的目标。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;比如说让自己的公司在今年之内成为某地区最有影响力的。&lt;/p&gt;

&lt;p&gt;但是后面紧跟着的KR一定要是清晰具体可衡量的，要讲这个抽象目标翻译和拆解为一个个具体的可以执行的行为要求。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;任何任务都应该加一个时间限制&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;否则就会导致任务堆积，拖延。不要等到所有任务都变成重要紧急的时候才想到去解决，而是要在设定任务的时候就规定一个完成时间。&lt;/p&gt;

&lt;p&gt;时间可以调整，任务可以顺延，但是不可以再遇到困难之后就将任务终止或者直接从任务列表中删除。&lt;/p&gt;

</description>
        <pubDate>Wed, 27 Nov 2019 10:46:05 +0000</pubDate>
        <link>https://findwisdom.github.io//2019/11/27/smart-principle/</link>
        <guid isPermaLink="true">https://findwisdom.github.io//2019/11/27/smart-principle/</guid>
        
        <category>学习笔记</category>
        
        <category>管理</category>
        
        <category>心理学</category>
        
        
        <category>管理</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-变量的作用域&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;li&gt;&lt;a href=&quot;#3-面试常考&quot; id=&quot;markdown-toc-3-面试常考&quot;&gt;3. 面试常考&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;你真的了解闭包吗，你知道闭包在实际开发中有什么用么，闭包是否会造成内存泄漏呢。&lt;/p&gt;

&lt;p&gt;如果你想了更进一步了解闭包，请接着往下看吧。&lt;/p&gt;

&lt;p&gt;本文主要参考《JavaScript设计模式与开发实践》。&lt;/p&gt;

&lt;h3 id=&quot;一-前言&quot;&gt;(一) 前言：&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;闭包是一个可以访问到其他函数内部变量的函数&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;闭包一直在前端圈内耳熟能详，甚至可以说是面试必考经典。&lt;/p&gt;

&lt;p&gt;但在我的前端程序员生涯的很长一段时间里，我对闭包的印象就是，我们在们面试中常说的那句。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;闭包是一个可以访问到其他函数内部变量的函数&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;随着开发项目的不断增加，经验的不断积累偶尔也会用到闭包，但对闭包的感知是零零碎碎不成体系的。于是有了总结闭包这个知识点的想法。&lt;/p&gt;

&lt;p&gt;本文是我个人依据个人日常开发经验，网络查询资料，综合整理的一篇文章，希望能给大家带来一些帮助&lt;/p&gt;

&lt;h3 id=&quot;二-闭包&quot;&gt;(二) 闭包&lt;/h3&gt;

&lt;p&gt;首先直观来看闭包的确是我们在面试中常说的那句，闭包是一个可以访问到其他函数内部变量的函数。&lt;/p&gt;

&lt;p&gt;为何会有上面那句话，细化来说个人认为理解两个点就好了，其一是&lt;strong&gt;变量的作用域&lt;/strong&gt;，是二是&lt;strong&gt;变量的生命周期&lt;/strong&gt;&lt;/p&gt;

&lt;h4 id=&quot;1-变量的作用域&quot;&gt;1. 变量的作用域&lt;/h4&gt;

&lt;p&gt;首先函数内的变量是局部变量，在该函数外我们是无法访问到的(下面的例子解释了这一点)&lt;/p&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;var&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;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;var&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;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;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&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;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 输出: func 1&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;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;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 输出: ReferenceError: a is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;作用域&lt;/strong&gt;的概念我想大家都知道，就不详细叙述了。&lt;/p&gt;

&lt;h4 id=&quot;2-变量的生命周期&quot;&gt;2. 变量的生命周期&lt;/h4&gt;

&lt;p&gt;变量一般在函数执行完毕后如果没有额外引用，就被销毁了，下面的例子可以比较清晰的反应出这一点&lt;/p&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;let&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;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;a&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;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&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;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;输出内容&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&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;p&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;c1&quot;&gt;// 输出内容 1&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;c1&quot;&gt;// 输出内容 1&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;c1&quot;&gt;// 输出内容 1&lt;/span&gt;
    
    &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;func1&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;a&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;k&quot;&gt;return&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;a&lt;/span&gt;&lt;span class=&quot;o&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;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;输出内容&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&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;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;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;funcClosure&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;func1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    
    &lt;span class=&quot;nx&quot;&gt;funcClosure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 输出内容 1&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;funcClosure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 输出内容 2&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;funcClosure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 输出内容 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;为什么闭包中的变量没被销毁呢这个涉及到&lt;strong&gt;垃圾回收机制&lt;/strong&gt;，即我们常说的&lt;strong&gt;标记清除&lt;/strong&gt;与&lt;strong&gt;引用计数&lt;/strong&gt;&lt;/p&gt;

&lt;h4 id=&quot;3-面试常考&quot;&gt;3. 面试常考&lt;/h4&gt;

&lt;p&gt;曾经面试喜欢考这样一道题目, 当然一般的题目给有序的DOM节点绑定&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;click事件&lt;/code&gt;；&lt;/p&gt;

&lt;p&gt;onclick的事件是异步触发的, for循环以迅雷不及掩耳的速度执行完毕所以每次打印的是最终的数字&lt;/p&gt;

&lt;p&gt;然而今天变量申明用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;let&lt;/code&gt;, 这一问题得到了完美的解决， 下面的例子用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setTimeout&lt;/code&gt;模拟异步执行&lt;/p&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;closure&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;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&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;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&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;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;setTimeout&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;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;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;输出内容&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 全部输出10&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;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;closure&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;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;closure1&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;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&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;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&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;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;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&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;setTimeout&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;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;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;输出内容&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&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;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;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 最后输出1, 2, 3, 4, 5 ... 10&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;closure1&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>Mon, 08 Jul 2019 17:35:05 +0000</pubDate>
        <link>https://findwisdom.github.io//2019/07/08/closure-func/</link>
        <guid isPermaLink="true">https://findwisdom.github.io//2019/07/08/closure-func/</guid>
        
        <category>闭包</category>
        
        <category>高阶函数</category>
        
        
        <category>JavaScript</category>
        
      </item>
    
      <item>
        <title>关于HTTP缓存</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;#缓存的优势&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;#私有浏览器缓存&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;/ul&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;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;li&gt;&lt;a href=&quot;#3-启发式缓存阶段&quot; id=&quot;markdown-toc-3-启发式缓存阶段&quot;&gt;3. 启发式缓存阶段&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#四-http中和缓存相关的首部字段&quot; id=&quot;markdown-toc-四-http中和缓存相关的首部字段&quot;&gt;(四) 🦀HTTP中和缓存相关的首部字段&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;li&gt;&lt;a href=&quot;#3-请求首部字段&quot; id=&quot;markdown-toc-3-请求首部字段&quot;&gt;3. 请求首部字段&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#4-实体首部字段&quot; id=&quot;markdown-toc-4-实体首部字段&quot;&gt;4. 实体首部字段&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;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1-cache-control&quot; id=&quot;markdown-toc-1-cache-control&quot;&gt;1. Cache-Control&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;

&lt;p&gt;缓存从来都是前端的一个痛点，很多前端搞不太清楚缓存到底是何物，从而给自己创造了一些麻烦，&lt;/p&gt;

&lt;p&gt;本文参考许多资料归纳总结，希望看完能对您有所帮助。&lt;/p&gt;

&lt;h3 id=&quot;一-前言&quot;&gt;(一) 前言：&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;掘金社区的: &lt;a href=&quot;https://juejin.im/post/5a6c87c46fb9a01ca560b4d7&quot;&gt;缓存详解&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;MDN Web 文档 &lt;a href=&quot;https://juejin.im/post/5a6c87c46fb9a01ca560b4d7&quot;&gt;HTTP 缓存&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;缓存是一种保存资源副本并且在下次请求时直接使用该副本的技术&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;说实话，我起始真的不知道怎么去介绍缓存，所以引用了上面相对官方的定义。&lt;/p&gt;

&lt;p&gt;我想几乎每个开发者都碰到过缓存的问题吧，甚至有很多情况下我们会说这个问题已经修复了,你清理下缓存就好了。&lt;/p&gt;

&lt;p&gt;这篇文章我们就细细的来挖掘下缓存的种种轶事。&lt;/p&gt;

&lt;h3 id=&quot;二-缓存种类&quot;&gt;(二) 🦋缓存种类：&lt;/h3&gt;

&lt;p&gt;很多开发者习惯把&lt;strong&gt;cookie&lt;/strong&gt;、&lt;strong&gt;webStorage&lt;/strong&gt;以及&lt;strong&gt;IndexedDB&lt;/strong&gt;存储的数据也称之为缓存，理由是都是保存在客户端的数据，没有什么区别。&lt;/p&gt;

&lt;p&gt;其实这是不严谨的，cookie的存在更多的是为了让服务端区别用户，webStorage和IndexedDB则更多用在保存具体的数据和在客户端存储大量结构化数据(文件/blobs)上面。&lt;/p&gt;

&lt;p&gt;缓存只有一种——&lt;strong&gt;它是请求资源的副本&lt;/strong&gt;&lt;/p&gt;

&lt;h4 id=&quot;缓存的优势&quot;&gt;缓存的优势&lt;/h4&gt;

&lt;p&gt;纳尼？你问我为什么要缓存？😱&lt;/p&gt;

&lt;p&gt;那就太容易说道了🤣，缓存好处有很多：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;缓解服务器压力(不用每次去请求资源)；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;提升性能(打开本地资源速度当然比请求回来再打开要快得多)；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;减少带宽消耗(我相信你可以理解)；&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;宏观分类&quot;&gt;宏观分类&lt;/h4&gt;

&lt;p&gt;缓存在宏观上可以分成两类：&lt;strong&gt;私有缓存&lt;/strong&gt;和&lt;strong&gt;共享缓存&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;共享缓存就是那些能被各级代理缓存的缓存(咋觉得有点绕)。&lt;/p&gt;

&lt;p&gt;共享缓存存储的响应能够被多个用户使用。私有缓存只能用于单独用户。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://findwisdom.oss-cn-hongkong.aliyuncs.com/cache/HTTPCachtType.png&quot; alt=&quot;enter image description here&quot; /&gt;&lt;/p&gt;

&lt;h5 id=&quot;私有浏览器缓存&quot;&gt;(私有)浏览器缓存&lt;/h5&gt;

&lt;p&gt;私有缓存只能用于单独用户。你可能已经见过浏览器设置中的“缓存”选项。&lt;/p&gt;

&lt;p&gt;浏览器缓存拥有用户通过 HTTP 下载的所有文档。这些缓存为浏览过的文档提供向后/向前导航，保存网页，查看源码等功能，可以避免再次向服务器发起多余的请求。&lt;/p&gt;

&lt;p&gt;它同样可以提供缓存内容的离线浏览。&lt;/p&gt;

&lt;h5 id=&quot;共享代理缓存&quot;&gt;(共享)代理缓存&lt;/h5&gt;

&lt;p&gt;共享缓存可以被多个用户使用。&lt;/p&gt;

&lt;p&gt;例如，ISP CDN 或你所在的公司可能会架设一个 web 代理来作为本地网络基础的一部分提供给用户。&lt;/p&gt;

&lt;p&gt;这样热门的资源就会被重复使用，减少网络拥堵与延迟。&lt;/p&gt;

&lt;h3 id=&quot;三-浏览器的缓存策略&quot;&gt;(三) 🦄浏览器的缓存策略&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;一个检索请求的成功响应: 对于 GET请求，响应状态码为：200，则表示为成功。一个包含例如HTML文档，图片，或者文件的响应；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;不变的重定向: 响应状态码：301；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;可用缓存响应：响应状态码：304，这个存在疑问，Chrome会缓存304中的缓存设置，Firefox；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;错误响应: 响应状态码：404 的一个页面；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;不完全的响应: 响应状态码 206，只返回局部的信息；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;除了 GET 请求外，如果匹配到作为一个已被定义的cache键名的响应；&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;以上，对于我们可以和应该缓存的目标有个了解。🤗&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;浏览器对于缓存的处理是根据第一次请求资源时返回的响应头来确定的。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;那么浏览器怎么确定一个资源该不该缓存，如何去缓存呢❓响应头！响应头！响应头！重要的事情说三遍。✌️&lt;/p&gt;

&lt;p&gt;我们看🌰：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    Age:23146
    Cache-Control:max-age=2592000
    Date:Tue, 28 Nov 2017 12:26:41 GMT
    ETag:W/&quot;5a1cf09a-63c6&quot;
    Expires:Thu, 28 Dec 2017 05:27:45 GMT
    Last-Modified:Tue, 28 Nov 2017 05:14:02 GMT
    Vary:Accept-Encoding
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;1强制缓存&quot;&gt;1.强制缓存&lt;/h4&gt;

&lt;p&gt;以上请求头来自百度首页某个CSS文件的响应头。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Expires&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;是HTTP/1.0中的定义缓存的字段，它规定了缓存过期的一个&lt;strong&gt;绝对时间&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Cache-Control:max-age=2592000&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;是HTTP/1.1定义的关于缓存的字段，它规定了缓存过期的一个&lt;strong&gt;相对时间&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;优先级上当然是版本高的优先了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max-age &amp;gt; Expires&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;这就是&lt;strong&gt;强缓存阶段&lt;/strong&gt;，当浏览器再次试图访问这个CSS文件，发现有这个文件的缓存，&lt;/p&gt;

&lt;p&gt;那么就判断根据上一次的响应判断是否过期，如果没过期，使用缓存。&lt;/p&gt;

&lt;p&gt;Firefox浏览器表现为一个灰色的200状态码。&lt;/p&gt;

&lt;p&gt;Chrome浏览器状态码表现为:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;200 (from disk cache)或是200 OK (from memory cache)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;2协商缓存阶段&quot;&gt;2.协商缓存阶段&lt;/h4&gt;

&lt;p&gt;那么当这个CSS文件过期了怎么办?ETag和Last-Modified就该闪亮登场了。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Last-Modified&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这个字段是文件最后一次修改的时间；&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;ETag&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ETag是对文件的一个标记，嗯，可以这么说，具体生成方式HTTP并没有给出一个明确的方式，&lt;/p&gt;

&lt;p&gt;所以理论上只要不会重复生成方式无所谓，比如对资源内容使用抗碰撞散列函数，使用最近修改的时间戳的哈希值，甚至只是一个版本号。&lt;/p&gt;

&lt;p&gt;利用这两个字段浏览器可以进入&lt;strong&gt;协商缓存阶段&lt;/strong&gt;，当浏览器再次试图访问这个CSS文件，发现缓存过期，&lt;/p&gt;

&lt;p&gt;于是会在本次请求的请求头里携带&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;If-Moified-Since&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;If-None-Match&lt;/code&gt;这两个字段，&lt;/p&gt;

&lt;p&gt;服务器通过这两个字段来判断资源是否有修改，如果有修改则返回状态码&lt;strong&gt;200和新的内容&lt;/strong&gt;，&lt;/p&gt;

&lt;p&gt;如果没有修改返回状态码&lt;strong&gt;304&lt;/strong&gt;，浏览器收到&lt;strong&gt;200&lt;/strong&gt;状态码，该咋处理就咋处理(相当于首次访问这个文件了)，&lt;/p&gt;

&lt;p&gt;发现返回&lt;strong&gt;304&lt;/strong&gt;，于是知道了本地缓存虽然过期但仍然可以用，于是加载本地缓存。然后根据新的返回的响应头来设置缓存。&lt;/p&gt;

&lt;p&gt;(这一步有所差异，发现不同浏览器的处理是不同的，chrome会为304设置缓存，firefox则不会)😑&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    If-Moified-Since: Tue, 28 Nov 2017 05:14:02 GMT
    If-None-Match: W/&quot;5a1cf09a-63c6&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;到这协商缓存结束&lt;/p&gt;

&lt;h4 id=&quot;3-启发式缓存阶段&quot;&gt;3. 启发式缓存阶段&lt;/h4&gt;

&lt;p&gt;我们把上面的响应头改下：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    Age:23146
    Cache-Control: public
    Date:Tue, 28 Nov 2017 12:26:41 GMT
    Last-Modified:Tue, 28 Nov 2017 05:14:02 GMT
    Vary:Accept-Encoding

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;发现没？浏览器用来确定缓存过期时间的字段一个都没有！那该怎么办？&lt;/p&gt;

&lt;p&gt;有人可能会说下次请求直接进入协商缓存阶段，携带&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;If-Moified-Since&lt;/code&gt;呗，不是的，浏览器还有个启发式缓存阶段😎&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;根据响应头中2个时间字段 Date 和 Last-Modified 之间的时间差值，取其值的10%作为缓存时间周期。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;看下面这张图，来解释浏览器整个缓存策略的过程：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://findwisdom.oss-cn-hongkong.aliyuncs.com/cache/Free-Converter.com-16137f262e0adf18-2103853.jpg&quot; alt=&quot;enter image description here&quot; /&gt;&lt;/p&gt;

&lt;p&gt;👌对于缓存策略介绍到这，接下来再细细分析不同的HTTP首部字段的内容，以及它们之间的关系。&lt;/p&gt;

&lt;h3 id=&quot;四-http中和缓存相关的首部字段&quot;&gt;(四) 🦀HTTP中和缓存相关的首部字段&lt;/h3&gt;

&lt;p&gt;HTTP报文是什么呢？就是HTTP报文，这是一个概念，主要由以下两部分构成：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;首部(header)&lt;/strong&gt;：包含了很多字段，比如：cookie、缓存、报文大小、报文格式等等）；&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;主体(body)&lt;/strong&gt;：HTTP请求真正要传输的部分，比如：一个HTML文档，一个js文件；&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;以上我们知道浏览器对于缓存的处理过程，也简单的提到了几个相关的字段。🤧接下来我们具体看下这几个字段：&lt;/p&gt;

&lt;h4 id=&quot;1-通用首部字段&quot;&gt;1. 通用首部字段&lt;/h4&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;字段名称&lt;/th&gt;
      &lt;th&gt;说明&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Cache-Control&lt;/td&gt;
      &lt;td&gt;控制缓存具体的行为&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pragma&lt;/td&gt;
      &lt;td&gt;HTTP1.0时的遗留字段，当值为”no-cache”时强制验证缓存&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Date&lt;/td&gt;
      &lt;td&gt;创建报文的日期时间(启发式缓存阶段会用到这个字段)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h4 id=&quot;2-响应首部字段&quot;&gt;2. 响应首部字段&lt;/h4&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;字段名称&lt;/th&gt;
      &lt;th&gt;说明&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;ETag&lt;/td&gt;
      &lt;td&gt;服务器生成资源的唯一标识&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Vary&lt;/td&gt;
      &lt;td&gt;代理服务器缓存的管理信息&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Age&lt;/td&gt;
      &lt;td&gt;资源在缓存代理中存贮的时长(取决于max-age和s-maxage的大小)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h4 id=&quot;3-请求首部字段&quot;&gt;3. 请求首部字段&lt;/h4&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;字段名称&lt;/th&gt;
      &lt;th&gt;说明&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;If-Match&lt;/td&gt;
      &lt;td&gt;条件请求，携带上一次请求中资源的ETag，服务器根据这个字段判断文件是否有新的修改&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;If-None-Match&lt;/td&gt;
      &lt;td&gt;和If-Match作用相反，服务器根据这个字段判断文件是否有新的修改&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;If-Modified-Since&lt;/td&gt;
      &lt;td&gt;比较资源前后两次访问最后的修改时间是否一致&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;If-Modified-Since&lt;/td&gt;
      &lt;td&gt;比较资源前后两次访问最后的修改时间是否一致&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h4 id=&quot;4-实体首部字段&quot;&gt;4. 实体首部字段&lt;/h4&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;字段名称&lt;/th&gt;
      &lt;th&gt;说明&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Expires&lt;/td&gt;
      &lt;td&gt;告知客户端资源缓存失效的绝对时间&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Last-Modified&lt;/td&gt;
      &lt;td&gt;资源最后一次修改的时间&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;五-浏览器缓存控制&quot;&gt;(五) 🦅浏览器缓存控制&lt;/h3&gt;

&lt;p&gt;HTTP/1.1一共规范了47种首部字段，而和缓存相关的就有以上12个之多。接下来的两个小节会一个一个介绍给大家。🤓&lt;/p&gt;

&lt;h4 id=&quot;1-cache-control&quot;&gt;1. Cache-Control&lt;/h4&gt;

&lt;p&gt;通过cache-control的指令可以控制告诉客户端或是服务器如何处理缓存。这也是11个字段中指令最多的一个，我们先来看看&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;请求指令&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;指令&lt;/th&gt;
      &lt;th&gt;参数&lt;/th&gt;
      &lt;th&gt;说明&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;no-cache&lt;/td&gt;
      &lt;td&gt;无&lt;/td&gt;
      &lt;td&gt;强制源服务器再次验证&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;no-store&lt;/td&gt;
      &lt;td&gt;无&lt;/td&gt;
      &lt;td&gt;不缓存请求或是响应的任何内容&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;max-age=[秒]&lt;/td&gt;
      &lt;td&gt;缓存时长，单位是秒&lt;/td&gt;
      &lt;td&gt;缓存的时长，也是响应的最大的Age值&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;min-fresh=[秒]&lt;/td&gt;
      &lt;td&gt;必需&lt;/td&gt;
      &lt;td&gt;期望在指定时间内响应仍然有效&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;no-transform&lt;/td&gt;
      &lt;td&gt;无&lt;/td&gt;
      &lt;td&gt;代理不可更改媒体类型&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;only-if-cached&lt;/td&gt;
      &lt;td&gt;无&lt;/td&gt;
      &lt;td&gt;从缓存获取&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;cache-extension&lt;/td&gt;
      &lt;td&gt;无&lt;/td&gt;
      &lt;td&gt;新的指令标记(token)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;响应指令&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;指令&lt;/th&gt;
      &lt;th&gt;参数&lt;/th&gt;
      &lt;th&gt;说明&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;public&lt;/td&gt;
      &lt;td&gt;无&lt;/td&gt;
      &lt;td&gt;任意一方都能缓存该资源(客户端、代理服务器等)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;private&lt;/td&gt;
      &lt;td&gt;无&lt;/td&gt;
      &lt;td&gt;只能特定用户缓存该资源&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;no-cache&lt;/td&gt;
      &lt;td&gt;无&lt;/td&gt;
      &lt;td&gt;强制源服务器再次验证&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;no-store&lt;/td&gt;
      &lt;td&gt;无&lt;/td&gt;
      &lt;td&gt;不缓存请求或是响应的任何内容&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;max-age=[秒]&lt;/td&gt;
      &lt;td&gt;缓存时长，单位是秒&lt;/td&gt;
      &lt;td&gt;缓存的时长，也是响应的最大的Age值&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;min-fresh=[秒]&lt;/td&gt;
      &lt;td&gt;必需&lt;/td&gt;
      &lt;td&gt;期望在指定时间内响应仍然有效&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;no-transform&lt;/td&gt;
      &lt;td&gt;无&lt;/td&gt;
      &lt;td&gt;代理不可更改媒体类型&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;must-revalidate&lt;/td&gt;
      &lt;td&gt;无&lt;/td&gt;
      &lt;td&gt;可缓存但必须再向源服务器进确认&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;proxy-revalidate&lt;/td&gt;
      &lt;td&gt;无&lt;/td&gt;
      &lt;td&gt;要求中间缓存服务器对缓存的响应有效性再进行确认&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;cache-extension&lt;/td&gt;
      &lt;td&gt;无&lt;/td&gt;
      &lt;td&gt;新的指令标记(token)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;请注意no-cache指令很多人误以为是不缓存，这是不准确的，no-cache的意思是可以缓存，但每次用应该去想服务器验证缓存是否可用。&lt;/p&gt;

&lt;p&gt;no-store才是不缓存内容。另外部分指令也可以组合使用，比如：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    Cache-Control: max-age=100, must-revalidate, public
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;上面指令的意思是缓存的有效时间为100秒，之后访问需要向源服务器发送请求验证，此缓存可被代理服务器和客户端缓存&lt;/p&gt;

&lt;h3 id=&quot;六--用户操作行为对缓存的影响&quot;&gt;(六) 🐲 用户操作行为对缓存的影响&lt;/h3&gt;

&lt;p&gt;搜索了很久有没有关于这方面的权威总结，最后竟然在百度百科找到了也是很惊讶，&lt;/p&gt;

&lt;p&gt;我自己加了一条用户强制刷新操作浏览器的反应。强制刷新，window下是&lt;strong&gt;Ctrl+F5&lt;/strong&gt;,mac下就是&lt;strong&gt;command+shift+R&lt;/strong&gt;操作了。:relieved:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;用户对浏览器操作&lt;/th&gt;
      &lt;th&gt;说明&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;打开新窗口&lt;/td&gt;
      &lt;td&gt;如果指定cache-control的值为private、no-cache、must-revalidate,那么打开新窗口访问时都会重新访问服务器。而如果指定了max-age值,那么在此值内的时间里就不会重新访问服务器,例如：Cache-control: max-age=5 表示当访问此网页后的5秒内不会去再次访问服务器.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;在地址栏回车&lt;/td&gt;
      &lt;td&gt;如果值为private或must-revalidate,则只有第一次访问时会访问服务器,以后就不再访问。如果值为no-cache,那么每次都会访问。如果值为max-age,则在过期之前不会重复访问。&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;按后退按扭&lt;/td&gt;
      &lt;td&gt;如果值为private、must-revalidate、max-age,则不会重访问,而如果为no-cache,则每次都重复访问.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;按刷新按扭&lt;/td&gt;
      &lt;td&gt;无论为何值,都会重复访问.（可能返回状态码：200、304，这个不同浏览器处理是不一样的，FireFox正常，Chrome则会启用缓存(200 from cache)）&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;按强制刷新按钮&lt;/td&gt;
      &lt;td&gt;当做首次进入重新请求(返回状态码200)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;:wink:如果想在浏览器点击“刷新”按钮的时候不让浏览器去发新的验证请求呢？办法找到一个，知乎上面一个回答，在页面加载完毕后通过脚本动态地添加资源：&lt;/p&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;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&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;load&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;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;bg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;http://img.infinitynewtab.com/wallpaper/100.jpg&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  	&lt;span class=&quot;nx&quot;&gt;setTimeout&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;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;#bgOut&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;css&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;background-image&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;url(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;dl&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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href=&quot;https://www.zhihu.com/question/28725359/answer/41960936&quot;&gt;来自知乎&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Thu, 06 Jun 2019 17:35:05 +0000</pubDate>
        <link>https://findwisdom.github.io//2019/06/06/cache/</link>
        <guid isPermaLink="true">https://findwisdom.github.io//2019/06/06/cache/</guid>
        
        <category>cache</category>
        
        <category>HTTP</category>
        
        
        <category>HTTP</category>
        
      </item>
    
      <item>
        <title>Git 仓库迁移</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#一-如何迁移git项目地址&quot; id=&quot;markdown-toc-一-如何迁移git项目地址&quot;&gt;(一) 如何迁移git项目地址：&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;最近迁移git 项目地址。&lt;/p&gt;

&lt;p&gt;想保留原有分支和提交&lt;/p&gt;

&lt;p&gt;网上搜索了好多步骤都很繁琐。最后发现了最优的解决方案。&lt;/p&gt;

&lt;h3 id=&quot;一-如何迁移git项目地址&quot;&gt;(一) 如何迁移git项目地址：&lt;/h3&gt;

&lt;p&gt;1.先克隆老项目的镜像&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    git clone --mirror old.git // old.git 为老项目的git地址
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;2.进入老项目的目录&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    cd old.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;3.移除老项目的地址替换成新项目&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    git remote set-url --push origin  new.git  //new.git 为新项目的git地址
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;4.将镜像推到远程&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    git push --mirror  // 这一步需要输入新的git的账号和密码
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;四步就搞定了。&lt;/p&gt;

</description>
        <pubDate>Tue, 04 Jun 2019 17:35:05 +0000</pubDate>
        <link>https://findwisdom.github.io//2019/06/04/git-change/</link>
        <guid isPermaLink="true">https://findwisdom.github.io//2019/06/04/git-change/</guid>
        
        <category>git</category>
        
        
        <category>git</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;/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语句频度tn&quot; id=&quot;markdown-toc-1语句频度tn&quot;&gt;1.语句频度T(n)&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;li&gt;&lt;a href=&quot;#3常见的时间复杂度有&quot; id=&quot;markdown-toc-3常见的时间复杂度有&quot;&gt;3.常见的时间复杂度有&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#4均时间复杂度和最坏时间复杂度&quot; id=&quot;markdown-toc-4均时间复杂度和最坏时间复杂度&quot;&gt;4.均时间复杂度和最坏时间复杂度&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;/ul&gt;

&lt;p&gt;最近打算学习一下算法，以下记录我的学习之路。&lt;/p&gt;

&lt;h3 id=&quot;一-算法的概念&quot;&gt;(一) 算法的概念：&lt;/h3&gt;

&lt;p&gt;算法 (Algorithm)，是对特定问题求解步骤的一种描述。&lt;/p&gt;

&lt;p&gt;解决一个问题往往有不止一种方法，算法也是如此。那么解决特定问题的多个算法之间如何衡量它们的优劣呢？有如下的指标：&lt;/p&gt;

&lt;h3 id=&quot;二-衡量算法的指标&quot;&gt;(二) 衡量算法的指标&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1.时间复杂度：执行这个算法需要消耗多少时间&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.空间复杂度：这个算法需要占用多少内存空间&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;同一个问题可以用不同的算法解决，而一个算法的优劣将影响到算法乃至程序的效率。算法分析的目的在于为特定的问题选择合适算法。&lt;strong&gt;一个算法的评价主要从时间复杂度和空间复杂度来考虑&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;算法在时间的高效性和空间的高效性之间通常是矛盾的&lt;/strong&gt;。所以一般只会取一个平衡点。通常我们假设程序运行在足够大的内存空间中，&lt;strong&gt;所以研究更多的是算法的时间复杂度&lt;/strong&gt;&lt;/p&gt;

&lt;h3 id=&quot;三-算法的时间复杂度&quot;&gt;(三) 算法的时间复杂度&lt;/h3&gt;

&lt;h5 id=&quot;1语句频度tn&quot;&gt;1.语句频度T(n)&lt;/h5&gt;

&lt;p&gt;一个算法执行所花费的时间，从理论上是不能算出来的，必须上机运行测试才能知道。&lt;/p&gt;

&lt;p&gt;但我们不可能对每个算法都上机测试，只需知道哪个算法花费的时间多，哪个算法花费的时间少就可以了。&lt;/p&gt;

&lt;p&gt;而且&lt;strong&gt;一个算法花费的时间与算法中的基本操作语句的执行次数成正比例，哪个算法中语句执行次数多，它花费时间就多&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;一个算法中的语句执行次数称为语句频度，记为T(n)。&lt;/p&gt;

&lt;h5 id=&quot;2时间复杂度&quot;&gt;2.时间复杂度&lt;/h5&gt;

&lt;p&gt;在刚才提到的语句频度中，n称为问题的规模，当n不断变化时，语句频度T(n)也会不断变化。&lt;/p&gt;

&lt;p&gt;但有时我们想知道它的变化呈现什么规律。为此，我们引入时间复杂度概念。&lt;/p&gt;

&lt;p&gt;一般情况下，算法中的基本操作语句的重复执行次数是问题规模n的某个函数，用T(n)表示，若有某个辅助函数f(n)，使得当n趋近于无穷大时，T(n) / f(n) 的极限值为不等于零的常数，则称f(n)是T(n)的同数量级函数。&lt;/p&gt;

&lt;p&gt;记作 T(n)=Ｏ( f(n) )，称Ｏ( f(n) )  为算法的渐进时间复杂度，简称时间复杂度。&lt;/p&gt;

&lt;p&gt;T(n) 不同，但时间复杂度可能相同。 如：T(n)=n²+5n+6 与 T(n)=3n²+3n+2 它们的T(n) 不同，但时间复杂度相同，都为O(n²)。&lt;/p&gt;

&lt;h5 id=&quot;3常见的时间复杂度有&quot;&gt;3.常见的时间复杂度有&lt;/h5&gt;

&lt;p&gt;常数阶O(1)，对数阶O(log2n)，线性阶O(n)，线性对数阶O(nlog2n)，平方阶O(n2)，立方阶O(n^3)， k次方阶O(nk)，指数阶O(2^n)。随着问题规模n的不断增大，上述时间复杂度不断增大，算法的执行效率越低。&lt;/p&gt;

&lt;h5 id=&quot;4均时间复杂度和最坏时间复杂度&quot;&gt;4.均时间复杂度和最坏时间复杂度&lt;/h5&gt;

&lt;p&gt;平均时间复杂度是指所有可能的输入实例均以等概率出现的情况下，该算法的运行时间&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;最坏情况下的时间复杂度称最坏时间复杂度&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;一般讨论的时间复杂度均是最坏情况下的时间复杂度。 这样做的原因是：最坏情况下的时间复杂度是算法在任何输入实例上运行时间的界限，这就保证了算法的运行时间不会比最坏情况更长&lt;/p&gt;

&lt;h5 id=&quot;5如何求时间复杂度&quot;&gt;5.如何求时间复杂度：&lt;/h5&gt;

&lt;p&gt;&lt;strong&gt;例子1&lt;/strong&gt;　&lt;/p&gt;

&lt;p&gt;如果算法的执行时间不随着问题规模n的增加而增长，即使算法中有上千条语句，其执行时间也不过是一个较大的常数。此类算法的时间复杂度是O(1)。&lt;/p&gt;

&lt;div class=&quot;language-java 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;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;91&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--;&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++;&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;该算法的时间复杂度为：O(1)&lt;/p&gt;

&lt;p&gt;这个程序看起来有点吓人，总共循环运行了1100次，但是我们看到n没有?&lt;/p&gt;

&lt;p&gt;没。这段程序的运行是和n无关的，就算它再循环一万年，我们也不管他，只是一个常数阶的函数&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;例子2&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-java 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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&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;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&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;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&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;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&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;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++;&lt;/span&gt;
         &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
     &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;该算法的时间复杂度为：O(n^3)&lt;/p&gt;

&lt;p&gt;该程序段中频度最大的语句是第5行的语句，内循环的执行次数虽然与问题规模n没有直接关系，&lt;/p&gt;

&lt;p&gt;但是却与外层循环的变量取值有关，而最外层循环的次数直接与n有关，因此该程序段的时间复杂度为 O(n^3)&lt;/p&gt;

&lt;p&gt;当有若干个循环语句时，算法的时间复杂度是由嵌套层数最多的循环语句中最内层语句的频度f(n)决定的。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;例子3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;算法的时间复杂度不仅仅依赖于问题的规模，还与输入实例的初始状态有关。
　　
在数值 A[n-1，n-2 …0] 中查找给定值k的算法大致如下&lt;/p&gt;

&lt;div class=&quot;language-java 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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&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;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
     &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;该算法的时间复杂度为：O(n)
  　　
此算法中第3行语句的频度不仅与问题规模n有关，&lt;/p&gt;

&lt;p&gt;还与输入实例A中的各元素取值和k的取值有关：如果A中没有与k相等的元素，那么第3行语句的频度为 f(n)=n ，该程序段的时间复杂度为 O(n)&lt;/p&gt;

&lt;p&gt;可查看&lt;a href=&quot;https://www.cnblogs.com/nnngu/p/8245787.html&quot;&gt;原文&lt;/a&gt;&lt;/p&gt;

</description>
        <pubDate>Fri, 17 May 2019 17:35:05 +0000</pubDate>
        <link>https://findwisdom.github.io//2019/05/17/arithmetic/</link>
        <guid isPermaLink="true">https://findwisdom.github.io//2019/05/17/arithmetic/</guid>
        
        <category>算法</category>
        
        
        <category>算法</category>
        
      </item>
    
      <item>
        <title>冒泡排序（Bubble Sort）</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;p&gt;最近打算学习一下算法，以下记录我的学习之路。&lt;/p&gt;

&lt;h3 id=&quot;一-简单的冒泡排序实现&quot;&gt;(一) 简单的冒泡排序实现&lt;/h3&gt;

&lt;p&gt;冒泡排序它的思想最简单，重复走过要排序的序列，一次比较两个元素，如果顺序错误就把它们调整过来（升序）。一直重复工作，直到把最大的元素一步步下沉到数组的尾部。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;什么时候最快（Best Cases）：&lt;/strong&gt;
当输入的数据已经是正序时（都已经是正序了，我还要你冒泡排序有何用啊。。。。）&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;什么时候最慢（Worst Cases）：&lt;/strong&gt;
当输入的数据是反序时（写一个for循环反序输出数据不就行了，干嘛要用你冒泡排序呢，我是闲的吗。。。&lt;/p&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;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;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&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;4&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;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&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;exchangeItem&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;nx&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;right&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;arrRight&lt;/span&gt; &lt;span class=&quot;o&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;nx&quot;&gt;left&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;nx&quot;&gt;left&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;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;right&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;nx&quot;&gt;right&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;arrRight&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;bubbleSort&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;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;for&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;i&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;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&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;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&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;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&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;j&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;nx&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&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;nx&quot;&gt;length&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;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;j&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;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;nx&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;j&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;nx&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;j&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;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;exchangeItem&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;nx&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;j&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;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;p&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;nx&quot;&gt;bubbleSort&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;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;nx&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可查看&lt;a href=&quot;https://github.com/findwisdom/arithmetic-js/blob/master/sort/bubbleSort.js&quot;&gt;代码&lt;/a&gt;&lt;/p&gt;

</description>
        <pubDate>Wed, 15 May 2019 17:35:05 +0000</pubDate>
        <link>https://findwisdom.github.io//2019/05/15/arithmetic-bubble-sort/</link>
        <guid isPermaLink="true">https://findwisdom.github.io//2019/05/15/arithmetic-bubble-sort/</guid>
        
        <category>JavaScript</category>
        
        <category>算法</category>
        
        
        <category>算法</category>
        
      </item>
    
      <item>
        <title>Js之寄生组合继承</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;/ul&gt;

&lt;p&gt;前面我们介绍了组合继承&lt;/p&gt;

&lt;p&gt;组合继承是javascript最常用的继承模式,不过,它也有自己的不足。&lt;/p&gt;

&lt;h3 id=&quot;一-组合式继承的缺陷&quot;&gt;(一) 组合式继承的缺陷&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;组合继承无论在什么情况下,都会调用两次父类构造函数&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;一次是在创建子类原型的时候,另一次是在子类构造函数内部。&lt;/p&gt;

&lt;p&gt;子类最终会包含父类对象的全部实例属性,但我们不得不在调用子类构造函数时重写这些属性.请再看一次组合继承的例子:&lt;/p&gt;

&lt;p&gt;例子：&lt;/p&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;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;head&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;脑袋瓜子&lt;/span&gt;&lt;span class=&quot;dl&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;emotion&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;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;喜&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;怒&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;哀&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;乐&lt;/span&gt;&lt;span class=&quot;dl&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;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//将 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;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;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;吃吃喝喝&lt;/span&gt;&lt;span class=&quot;dl&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;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;sleep&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;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;睡觉&lt;/span&gt;&lt;span class=&quot;dl&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;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;run&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;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;快跑&lt;/span&gt;&lt;span class=&quot;dl&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;//子类：学生，继承了“人”这个类&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;studentID&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;studentID&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;studentID&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;call&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;c1&quot;&gt;// 第二次调用Person()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;nx&quot;&gt;Student&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;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;c1&quot;&gt;// 第一次调用Person()&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;Student&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;Student&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;//将 Student 原型对象的 constructor 指针重新指向 Student 本身&lt;/span&gt;
    
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;stu1&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;Student&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1001&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;nx&quot;&gt;stu1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;emotion&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;nx&quot;&gt;stu1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;emotion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;愁&lt;/span&gt;&lt;span class=&quot;dl&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;nx&quot;&gt;stu1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;emotion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//[&quot;喜&quot;, &quot;怒&quot;, &quot;哀&quot;, &quot;乐&quot;, &quot;愁&quot;]&lt;/span&gt;
    
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;stu2&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;Student&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1002&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;nx&quot;&gt;stu2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;emotion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//[&quot;喜&quot;, &quot;怒&quot;, &quot;哀&quot;, &quot;乐&quot;]&lt;/span&gt;
    
    &lt;span class=&quot;nx&quot;&gt;stu1&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;span class=&quot;nx&quot;&gt;stu2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;run&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;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;nx&quot;&gt;stu1&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;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;//Student&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在第一次调用Person构造函数时,Person.prototype会得到两个属性:head和emotion,他们都是Person的实例属性。&lt;/p&gt;

&lt;p&gt;只不过现在位于Student的原型中.当调用Student构造函数时,又会调用一次Person构造函数,这一次又在新对象上创建了实例属性head和emotion.于是,这两个属性就屏蔽了原型中的两个同名属性.&lt;/p&gt;

&lt;p&gt;结果是,有两组head和emotion属性,一组在Student的实例上,一组在Student的原型上。&lt;/p&gt;

&lt;p&gt;这就是调用两次Person构造函数的结果.而现在,找到了解决这个问题的方法:&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;寄生组合式继承&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;二-寄生组合式继承&quot;&gt;(二) 寄生组合式继承&lt;/h3&gt;

&lt;p&gt;寄生组合式继承:&lt;strong&gt;通过借用构造函数来继承属性,通过原型链的混成形式来继承方法&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;思路:不必为了指定子类的原型而调用父类的构造函数,我们所需要的无非就是父类原型的一个副本而已。&lt;/p&gt;

&lt;p&gt;本质上,就是使用寄生式继承来继承父类的原型,然后在将结果指定给子类的原型:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Object.create()&lt;/strong&gt; 方法创建一个新对象，使用现有的对象来提供新创建的对象的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;。&lt;/p&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;inheritPrototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;subType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;superType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
          &lt;span class=&quot;kd&quot;&gt;var&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;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;superType&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;//创建父类原型的一个副本 等同于使用Object.create(superType.prototype)&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;subType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;//为副本添加constructor属性,弥补重写原型而失去的constructor属性&lt;/span&gt;
          &lt;span class=&quot;nx&quot;&gt;subType&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;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;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样,我们就可以通过调用inheritPrototype()函数,替换前面例子中为子类原型的赋值语句了:&lt;/p&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;inheritPrototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;subType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;superType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
          &lt;span class=&quot;kd&quot;&gt;var&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;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;superType&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;//创建父类原型的一个副本 等同于使用Object.create(superType.prototype)&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;subType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;//为副本添加constructor属性,弥补重写原型而失去的constructor属性&lt;/span&gt;
          &lt;span class=&quot;nx&quot;&gt;subType&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;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;p&quot;&gt;}&lt;/span&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;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;head&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;脑袋瓜子&lt;/span&gt;&lt;span class=&quot;dl&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;emotion&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;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;喜&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;怒&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;哀&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;乐&lt;/span&gt;&lt;span class=&quot;dl&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;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//将 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;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;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;吃吃喝喝&lt;/span&gt;&lt;span class=&quot;dl&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;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;sleep&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;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;睡觉&lt;/span&gt;&lt;span class=&quot;dl&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;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;run&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;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;快跑&lt;/span&gt;&lt;span class=&quot;dl&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;//子类：学生，继承了“人”这个类&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;studentID&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;studentID&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;studentID&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;call&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;c1&quot;&gt;// 第二次调用Person()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;nx&quot;&gt;inheritPrototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Student&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;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;stu1&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;Student&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1001&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;nx&quot;&gt;stu1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;emotion&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;nx&quot;&gt;stu1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;emotion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;愁&lt;/span&gt;&lt;span class=&quot;dl&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;nx&quot;&gt;stu1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;emotion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//[&quot;喜&quot;, &quot;怒&quot;, &quot;哀&quot;, &quot;乐&quot;, &quot;愁&quot;]&lt;/span&gt;
    
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;stu2&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;Student&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1002&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;nx&quot;&gt;stu2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;emotion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//[&quot;喜&quot;, &quot;怒&quot;, &quot;哀&quot;, &quot;乐&quot;]&lt;/span&gt;
    
    &lt;span class=&quot;nx&quot;&gt;stu1&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;span class=&quot;nx&quot;&gt;stu2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;run&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;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;nx&quot;&gt;stu1&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;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;//Student&lt;/span&gt;
    
    &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;stu1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Student&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;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;stu1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&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;c1&quot;&gt;//true&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Student&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;isPrototypeOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;stu1&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;nx&quot;&gt;alert&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;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;isPrototypeOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;stu1&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这个例子的高效率体现在它只调用了一次Person构造函数,并且因此避免了在Student.prototype上创建不必要的 多余的属性。&lt;/p&gt;

&lt;p&gt;与此同时,原型链还能保持不变.因此,还能够正常使用instanceof 和isPrototypeOf确定继承关系。&lt;/p&gt;
</description>
        <pubDate>Wed, 08 May 2019 17:35:05 +0000</pubDate>
        <link>https://findwisdom.github.io//2019/05/08/prototype-group-parasitic/</link>
        <guid isPermaLink="true">https://findwisdom.github.io//2019/05/08/prototype-group-parasitic/</guid>
        
        <category>JavaScript</category>
        
        <category>prototype</category>
        
        <category>继承</category>
        
        
        <category>JavasSript</category>
        
      </item>
    
  </channel>
</rss>
