(window.webpackJsonp=window.webpackJsonp||[]).push([[33],{361:function(s,t,a){"use strict";a.r(t);var n=a(3),r=Object(n.a)({},(function(){var s=this,t=s._self._c;return t("ContentSlotsDistributor",{attrs:{"slot-key":s.$parent.slotKey}},[t("h1",{attrs:{id:"js设计模式总结笔记"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#js设计模式总结笔记"}},[s._v("#")]),s._v(" JS设计模式总结笔记")]),s._v(" "),t("h2",{attrs:{id:"开篇-前端工程师的成长论"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#开篇-前端工程师的成长论"}},[s._v("#")]),s._v(" 开篇:前端工程师的成长论")]),s._v(" "),t("p",[s._v("能够决定一个前端工程师的本质的,不是那些瞬息万变的技术点,而是那些"),t("strong",[s._v("不变的东西")]),s._v("。")]),s._v(" "),t("p",[s._v("所谓“不变的东西”,就是"),t("strong",[s._v("驾驭技术的能力")]),s._v("。")]),s._v(" "),t("p",[s._v("具体来说,它分为以下三个层次:")]),s._v(" "),t("ul",[t("li",[s._v("能用健壮的代码去解决具体的问题;")]),s._v(" "),t("li",[s._v("能用抽象的思维去应对复杂的系统;")]),s._v(" "),t("li",[s._v("能用工程化的思想去规划更大规模的业务。")])]),s._v(" "),t("blockquote",[t("p",[s._v("基础理论知识是一个人的基线,理论越强基线越高。再为自己定一个目标和向上攀附的阶梯,那么达到目标就是时间问题,而很多野路子工程师搞了半辈子也未达到优秀工程师的基线,"),t("strong",[s._v("很多他们绞尽脑汁得出的高深学问,不过是正规工程师看起来很自然的东西")]),s._v("。—— 吴军")])]),s._v(" "),t("h2",{attrs:{id:"设计模式之-道"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#设计模式之-道"}},[s._v("#")]),s._v(" 设计模式之“道”")]),s._v(" "),t("ul",[t("li",[s._v("设计模式是“拿来主义”。如使用数学公式,不会从头推导一个公式。")])]),s._v(" "),t("h3",{attrs:{id:"核心思想"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#核心思想"}},[s._v("#")]),s._v(" 核心思想")]),s._v(" "),t("ul",[t("li",[s._v("设计模式的核心思想——"),t("strong",[s._v("封装变化")])]),s._v(" "),t("li",[s._v("保证可维护性、可扩展性。")]),s._v(" "),t("li",[s._v("将变与不变分离,确保变化的部分灵活,不变的部分稳定。——这就是所谓的“健壮”的代码。")])]),s._v(" "),t("h2",{attrs:{id:"设计模式之-术"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#设计模式之-术"}},[s._v("#")]),s._v(" 设计模式之“术”")]),s._v(" "),t("p",[s._v("即最经典的"),t("strong",[s._v("23种设计模式")]),s._v("。按"),t("code",[s._v("创建型")]),s._v("、"),t("code",[s._v("结构型")]),s._v("、"),t("code",[s._v("行为型")]),s._v("划分。")]),s._v(" "),t("h3",{attrs:{id:"创建型"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#创建型"}},[s._v("#")]),s._v(" 创建型")]),s._v(" "),t("ol",[t("li",[s._v("单例模式")]),s._v(" "),t("li",[s._v("原型模式")]),s._v(" "),t("li",[s._v("构造器模式")]),s._v(" "),t("li",[s._v("工厂模式")]),s._v(" "),t("li",[s._v("抽象工厂模式")])]),s._v(" "),t("h3",{attrs:{id:"结构型"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#结构型"}},[s._v("#")]),s._v(" 结构型")]),s._v(" "),t("ol",[t("li",[s._v("桥接模式")]),s._v(" "),t("li",[s._v("外观模式")]),s._v(" "),t("li",[s._v("组合模式")]),s._v(" "),t("li",[s._v("装饰器模式")]),s._v(" "),t("li",[s._v("适配器模式")]),s._v(" "),t("li",[s._v("代理模式")]),s._v(" "),t("li",[s._v("享元模式")])]),s._v(" "),t("h3",{attrs:{id:"行为型"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#行为型"}},[s._v("#")]),s._v(" 行为型")]),s._v(" "),t("ol",[t("li",[s._v("迭代器模式")]),s._v(" "),t("li",[s._v("解释器模式")]),s._v(" "),t("li",[s._v("观察者模式")]),s._v(" "),t("li",[s._v("访问者模式")]),s._v(" "),t("li",[s._v("状态模式")]),s._v(" "),t("li",[s._v("备忘录模式")]),s._v(" "),t("li",[s._v("策略模式")]),s._v(" "),t("li",[s._v("模板方法模式")]),s._v(" "),t("li",[s._v("职责链模式")]),s._v(" "),t("li",[s._v("命令模式")])]),s._v(" "),t("h2",{attrs:{id:"小结"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#小结"}},[s._v("#")]),s._v(" 小结:")]),s._v(" "),t("ul",[t("li",[t("p",[s._v("创建型模型封装了创建对象过程中的变化。")])]),s._v(" "),t("li",[t("p",[s._v("结构型模式封装了对象之间组合方式的变化。目的在于灵活的表达对象间的配合与依赖关系。")])]),s._v(" "),t("li",[t("p",[s._v("行为型模式将对象千变万化的行为进行抽离,确保安全、方便的更改。")])])]),s._v(" "),t("h2",{attrs:{id:"创建型-工厂模式-简单工厂-区分-变与不变"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#创建型-工厂模式-简单工厂-区分-变与不变"}},[s._v("#")]),s._v(" 创建型:工厂模式-简单工厂——区分“变与不变”")]),s._v(" "),t("p",[s._v("先了解构造器模式,在JS中的构造函数即构造器。使用构造函数,即使用构造器模式。")]),s._v(" "),t("h3",{attrs:{id:"构造器模式"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#构造器模式"}},[s._v("#")]),s._v(" 构造器模式")]),s._v(" "),t("div",{staticClass:"language-js line-numbers-mode"},[t("pre",{pre:!0,attrs:{class:"language-js"}},[t("code",[t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("function")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token function"}},[s._v("User")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("(")]),t("span",{pre:!0,attrs:{class:"token parameter"}},[s._v("name"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" age"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" career")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(")")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("{")]),s._v("\n "),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("this")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(".")]),s._v("name "),t("span",{pre:!0,attrs:{class:"token operator"}},[s._v("=")]),s._v(" name\n "),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("this")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(".")]),s._v("age "),t("span",{pre:!0,attrs:{class:"token operator"}},[s._v("=")]),s._v(" age\n "),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("this")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(".")]),s._v("career "),t("span",{pre:!0,attrs:{class:"token operator"}},[s._v("=")]),s._v(" career\n"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("}")]),s._v("\n"),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("const")]),s._v(" user "),t("span",{pre:!0,attrs:{class:"token operator"}},[s._v("=")]),s._v(" New "),t("span",{pre:!0,attrs:{class:"token function"}},[s._v("User")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("(")]),t("span",{pre:!0,attrs:{class:"token string"}},[s._v("'张三'")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[s._v("18")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token string"}},[s._v("'前端工程师'")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(")")]),s._v("\n")])]),s._v(" "),t("div",{staticClass:"line-numbers-wrapper"},[t("span",{staticClass:"line-number"},[s._v("1")]),t("br"),t("span",{staticClass:"line-number"},[s._v("2")]),t("br"),t("span",{staticClass:"line-number"},[s._v("3")]),t("br"),t("span",{staticClass:"line-number"},[s._v("4")]),t("br"),t("span",{staticClass:"line-number"},[s._v("5")]),t("br"),t("span",{staticClass:"line-number"},[s._v("6")]),t("br")])]),t("p",[s._v("变与不变:user的属性(name, age, car)不变,即共性。变的是属性值,即个性。")]),s._v(" "),t("h3",{attrs:{id:"简单工厂模式"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#简单工厂模式"}},[s._v("#")]),s._v(" 简单工厂模式")]),s._v(" "),t("div",{staticClass:"language-js line-numbers-mode"},[t("pre",{pre:!0,attrs:{class:"language-js"}},[t("code",[t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("function")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token function"}},[s._v("User")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("(")]),t("span",{pre:!0,attrs:{class:"token parameter"}},[s._v("name "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" age"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" career"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" work")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(")")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("{")]),s._v("\n "),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("this")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(".")]),s._v("name "),t("span",{pre:!0,attrs:{class:"token operator"}},[s._v("=")]),s._v(" name\n "),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("this")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(".")]),s._v("age "),t("span",{pre:!0,attrs:{class:"token operator"}},[s._v("=")]),s._v(" age\n "),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("this")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(".")]),s._v("career "),t("span",{pre:!0,attrs:{class:"token operator"}},[s._v("=")]),s._v(" career\n "),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("this")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(".")]),s._v("work "),t("span",{pre:!0,attrs:{class:"token operator"}},[s._v("=")]),s._v(" work\n"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("}")]),s._v("\n\n"),t("span",{pre:!0,attrs:{class:"token comment"}},[s._v("// 工厂函数 (将变的部分抽离出一个函数)")]),s._v("\n"),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("function")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token function"}},[s._v("Factory")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("(")]),t("span",{pre:!0,attrs:{class:"token parameter"}},[s._v("name"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" age"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" career")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(")")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("{")]),s._v("\n "),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("let")]),s._v(" work\n "),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("switch")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("(")]),s._v("career"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(")")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("{")]),s._v("\n "),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("case")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token string"}},[s._v("'coder'")]),t("span",{pre:!0,attrs:{class:"token operator"}},[s._v(":")]),s._v("\n work "),t("span",{pre:!0,attrs:{class:"token operator"}},[s._v("=")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("[")]),t("span",{pre:!0,attrs:{class:"token string"}},[s._v("'写代码'")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),t("span",{pre:!0,attrs:{class:"token string"}},[s._v("'写系分'")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token string"}},[s._v("'修Bug'")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("]")]),s._v("\n "),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("break")]),s._v("\n "),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("case")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token string"}},[s._v("'product manager'")]),t("span",{pre:!0,attrs:{class:"token operator"}},[s._v(":")]),s._v("\n work "),t("span",{pre:!0,attrs:{class:"token operator"}},[s._v("=")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("[")]),t("span",{pre:!0,attrs:{class:"token string"}},[s._v("'订会议室'")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token string"}},[s._v("'写PRD'")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token string"}},[s._v("'催更'")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("]")]),s._v("\n "),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("break")]),s._v("\n "),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("case")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token string"}},[s._v("'boss'")]),t("span",{pre:!0,attrs:{class:"token operator"}},[s._v(":")]),s._v("\n work "),t("span",{pre:!0,attrs:{class:"token operator"}},[s._v("=")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("[")]),t("span",{pre:!0,attrs:{class:"token string"}},[s._v("'喝茶'")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token string"}},[s._v("'看报'")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token string"}},[s._v("'见客户'")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("]")]),s._v("\n\n "),t("span",{pre:!0,attrs:{class:"token comment"}},[s._v("// 其它工种的职责分配")]),s._v("\n "),t("span",{pre:!0,attrs:{class:"token comment"}},[s._v("//case 'xxx':")]),s._v("\n "),t("span",{pre:!0,attrs:{class:"token comment"}},[s._v("//...")]),s._v("\n\n "),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("return")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token keyword"}},[s._v("new")]),s._v(" "),t("span",{pre:!0,attrs:{class:"token class-name"}},[s._v("User")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("(")]),s._v("name"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" age"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" career"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(",")]),s._v(" work"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v(")")]),s._v("\n"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[s._v("}")]),s._v("\n")])]),s._v(" "),t("div",{staticClass:"line-numbers-wrapper"},[t("span",{staticClass:"line-number"},[s._v("1")]),t("br"),t("span",{staticClass:"line-number"},[s._v("2")]),t("br"),t("span",{staticClass:"line-number"},[s._v("3")]),t("br"),t("span",{staticClass:"line-number"},[s._v("4")]),t("br"),t("span",{staticClass:"line-number"},[s._v("5")]),t("br"),t("span",{staticClass:"line-number"},[s._v("6")]),t("br"),t("span",{staticClass:"line-number"},[s._v("7")]),t("br"),t("span",{staticClass:"line-number"},[s._v("8")]),t("br"),t("span",{staticClass:"line-number"},[s._v("9")]),t("br"),t("span",{staticClass:"line-number"},[s._v("10")]),t("br"),t("span",{staticClass:"line-number"},[s._v("11")]),t("br"),t("span",{staticClass:"line-number"},[s._v("12")]),t("br"),t("span",{staticClass:"line-number"},[s._v("13")]),t("br"),t("span",{staticClass:"line-number"},[s._v("14")]),t("br"),t("span",{staticClass:"line-number"},[s._v("15")]),t("br"),t("span",{staticClass:"line-number"},[s._v("16")]),t("br"),t("span",{staticClass:"line-number"},[s._v("17")]),t("br"),t("span",{staticClass:"line-number"},[s._v("18")]),t("br"),t("span",{staticClass:"line-number"},[s._v("19")]),t("br"),t("span",{staticClass:"line-number"},[s._v("20")]),t("br"),t("span",{staticClass:"line-number"},[s._v("21")]),t("br"),t("span",{staticClass:"line-number"},[s._v("22")]),t("br"),t("span",{staticClass:"line-number"},[s._v("23")]),t("br"),t("span",{staticClass:"line-number"},[s._v("24")]),t("br"),t("span",{staticClass:"line-number"},[s._v("25")]),t("br"),t("span",{staticClass:"line-number"},[s._v("26")]),t("br")])]),t("p",[s._v("工厂模式就是将创建对象的过程单独封装。")])])}),[],!1,null,null,null);t.default=r.exports}}]);