源码学习
2020-04-16 11:10:06 0 举报
AI智能生成
前端源码学习方法论
作者其他创作
大纲/内容
学习什么
架构设计思维
对所用工具更深的理解
优秀编码技巧
源码都有的特性
开始经常会有大量的健壮性判断
<font color="#f15a23">健壮性很重要</font>
<font color="#5c5c5c">1. 健壮性最有效的保证就是在</font><font color="#924517">参数层面</font><br><font color="#5c5c5c">2. 把</font><font color="#924517">容易出问题的代码块</font><font color="#5c5c5c">用</font><font color="#924517"> try catch 包裹</font><font color="#5c5c5c">起来</font>
模块调模块
看源码技巧
不要一句一句读
先理清架构,再看入口,依照流程读下去
摈弃其他代码,看核心代码
典型架构
工厂模式
工厂模式是我们最常用的实例化对象模式,<br>是<b><font color="#924517">用工厂方法代替new操作</font></b>的一种模式。<br>
工厂模式可以<b>解决实例化对象产生大量重复的问题</b>
<script type="text/javascript"><br> function createObject(name,age,profession){//集中实例化的函数<br> var obj =<font color="#924517"> new Object()</font>;<br> obj.name = name;<br> obj.age = age;<br> obj.profession = profession;<br> obj.move = function () {<br> return this.name + ' at ' + this.age + ' engaged in ' + this.profession;<br> };<br> return obj;<br> }<br> var test1 = createObject('trigkit4',22,'programmer');//第一个实例<br> var test2 = createObject('mike',25,'engineer');//第二个实例<br> alert(test1.move());<br> alert(test2.move());<br></script> <br>
类库例子: JQuery
优点
通过使用工程方法而不是new关键字,将所有实例化的代码集中在一个位置<b><font color="#924517">防止代码重复</font></b>
缺点
工厂模式解决了重复实例化的问题,但<b><font color="#924517">一定要查看工厂方法才知道是哪个对象的实例</font></b>;<br>相比之下<b>使用new关键字和构造函数,可以让代码更加简单易读。</b><br>
应用场景
<ul><li>当对象或组件涉及高复杂性时<br></li><li>当需要根据所在的不同环境轻松生成对象的不同实例时<br></li><li>当处理很多共享相同属性的小型对象或组件时<br></li></ul>
建造者模式
只需要产生少数对象,构造过程比较复杂
建造者模式将一个复杂对象的构建和其表示相分离,使得同样的构建过程可以创建不同的表示。<br>也就是说如果我们用了建造者模式,那么用户就需要制定需要建造的类型就可以得到他们,而具体建造的过程和细节就不需要知道了。<br>建造者模式<b>实际上就是一个指挥者,一个建造者,一个指挥者调用具体建造者工作得出结果</b>给客户。 <br><br>建造者模式<b><font color="#924517">主要用于”分步骤构建一个复杂的对象”</font></b>,在这其中”分步骤”是一个稳定的算法,而复杂对象的各个部分则经常变化。<br>
作用
1.<b>分步创建一个复杂的对象</b><br><br>2.解耦封装过程和具体创建组件<br><br>3.<b>无需关心组件如何组装</b><br>
注意事项
1.一定要一个稳定的算法进行支持<br><br>2.<b>加工工艺是暴露的</b>--白富美不用关心如何建房子,但可以随时去看房子建得怎么样<br>
类库例子: Vue
函数式编程
函数式编程是种编程方式,它<b>将电脑运算视为函数的计算</b>;<br>它属于"结构化编程"的一种,主要思想是<b>把运算过程尽量写成一系列嵌套的函数调用</b>。<br>
两个基本运算
<font color="#16884a">合成</font>
如果一个值要经过多个函数,才能变成另外一个值,<br>就可以把所有中间步骤合并成一个函数,这叫做"函数的合成"(compose)。<br><br>合成两个函数的简单代码如下。<br><br>const compose = function (f, g) {<br> return function (x) {<br> return f(g(x));<br> };<br>}<br>
<font color="#16884a">柯里化</font>
f(x)和g(x)合成为f(g(x)),有一个隐藏的前提,<b><font color="#924517">就是f和g都只能接受一个参数</font></b>。<br><b>如果可以接受多个参数,比如f(x, y)和g(a, b, c),函数合成就非常麻烦</b>。<br><br>这时就需要函数柯里化了。所谓<b><font color="#f1753f">"柯里化",就是把一个多参数的函数,转化为单参数函数</font></b>。<br>
// 柯里化之前<br>function add(x, y) {<br> return x + y;<br>}<br><br><b>add(1, 2) </b>// 3<br><br>// 柯里化之后<br>function addX(y) {<br> return function (x) {<br> return x + y;<br> };<br>}<br><br><b>addX(2)(1)</b> // 3<br>
类库例子: lodash
Vue3 为什么要改成函数式
Logic Composition<br>组合大于继承
One of the key aspects of the component API is how to<b><font color="#924517"> </font><font color="#16884a">encapsulate and reuse logic across multiple components</font></b>. <br>With Vue 2.x's current API, there are a number of common patterns we've seen in the past, each with its own drawbacks. <br>These include:<br><br><ul><li><font color="#f1753f">Mixins (via the mixins option)<br></font></li><li><font color="#f1753f">Higher-order components (HOCs)<br></font></li><li><font color="#f1753f">Renderless components (via scoped slots)</font><br><br></li></ul>There is plenty of information regarding these patterns on the internet, so we shall not repeat them in full details here. <br>In general, <b>these patterns all suffer from one or more of the drawbacks below</b>:<br><br><ul><li><b><font color="#924517">Unclear sources for properties exposed on the render context</font></b>. For example, when reading the template of a component using multiple mixins, it can be difficult to tell from which mixin a specific property was injected from.<br></li></ul><br><ul><li><b><font color="#924517">Namespace clashing</font></b>. Mixins can potentially clash on property and method names, while HOCs can clash on expected prop names.<br></li></ul><br><ul><li>Performance. <b>HOCs and renderless components<font color="#924517"> require extra stateful component instances</font></b> that come at a performance cost.</li></ul>
<b>The function based API,</b> inspired by React Hooks, <br><b><font color="#924517">presents a clean and flexible way to compose logic inside and between components without any of these drawbacks</font></b>. <br>
Type Inference<br>更好地支持 TS
One of the major goals of 3.0 is to<font color="#16884a"> <b>provide better built-in TypeScript type inference support</b>.</font> <br>Originally we tried to address this problem with the now-abandoned Class API RFC, <br>but after discussion and prototyping we discovered that <b><font color="#924517">using Classes doesn't fully address the typing issue</font></b>.<br><br><b><font color="#924517">The function-based APIs, on the other hand, are naturally type-friendly</font></b>. <br>In the prototype we have already achieved full typing support for the proposed APIs. <br>The best part is - code written in TypeScript will look almost identical to code written in plain JavaScript. <br>
Bundle Size<br>打包后体积更小
<b>Function-based APIs are exposed as named ES exports and imported on demand</b>. <br>This makes them<b><font color="#924517"> tree-shakable</font></b>, and <b>leaves more room for future API additions</b>. <br>Code written with function-based APIs also<b><font color="#924517"> compresses better</font></b> <b>than object-or-class-based code</b>,<br> since (with standard minification) <b>function and variable names can be shortened while object/class methods and properties cannot.</b><br>
参考
https://github.com/vuejs/rfcs/blob/function-apis/active-rfcs/0000-function-api.md
参考
http://www.ruanyifeng.com/blog/2017/02/fp-tutorial.html
JQuery 源码
(function(window){<br><br>})(window)<br>
<font color="#16884a">为什么要将 window 当参数传入</font><br>
<font color="#924517">减少作用域链的查找时间</font>
<font color="#16884a">如何给 JQuery 添加各种功能</font>
<b><font color="#924517">先构建 extend 方法,用 extend 来扩展模块</font></b>
jquery.extend=jquery.fn.extend=function(){<br><br>}<br>jquery.fn.extend({<br> // css 模块<br>})<br>jquery.fn.extend({<br> // 动画 模块<br>});<br>
extend 方法中的<font color="#16884a">享元模式</font>
<b>享元模式可以<font color="#f1753f">有效减少代码中的对象数量</font></b>
jquery.extend = jquery.fn.extend = function() {<br> // 享元模式,把不同部分拿出来,相同部分保留下<br> var <font color="#924517">target;</font><br> var <font color="#f1753f">source</font>;<br> if (arguments.length === 1) {<br> <font color="#924517">target = this</font>;<br> <font color="#f1753f">source=arguments[0]</font>;<br> }else{<br> <font color="#924517">target=arguments[0];</font><br> <font color="#f1753f"> source = arguments[1]</font>;<br>}<br><br> for (var name in source) {<br> target[name] = source[name];<br> }<br>};<br>
<font color="#16884a">模块化支持检测<br></font>(现在一定要有)
jquery 里:<br><br>if (typeof define === "function" && define.amd && define.amd.jquery) {<br> define("jquery", [], function() {<br> return jquery;<br> });<br>}<br>
Vue 里:<br><br>(function(global, factory) {<br> typeof exports === "object" && typeof module !== "undefined"<br> ? (module.exports = factory())<br> : typeof define === "function" && define.amd<br> ? define(factory)<br> : ((global = global || self), (global.Vue = factory()));<br>})(this, function() {})();<br>
架构亮点
利用工厂模式,无 new 化构建对象
模块划分明确
开闭原则的优秀体现<br>指的是 extend 方法吗??<br>
在面向对象编程领域中,开闭原则规定“软件中的对象(类,模块,函数等等)<br>应该<b>对于扩展是开放的,但是对于修改是封闭的”</b>,<br>这意味着一个实体是<b>允许在不改变它的源代码的前提下变更它的行为</b>。<br>
express 架构
<b><font color="#16884a">建造者模式,通过 mixin 函数来给 app 函数扩展功能<br></font></b><br>var mixin=require('merge-descriptors')<br>var proto=require('./application.js')<br>module.exports=createApplication<br><br>function createApplication(){<br> function app(){<br><br> }<br><b><font color="#924517"> mixin(app,proto)</font></b><br> return app<br>}<br>
给 app 函数对象上添加<br> get post delete 等请求方法<br>
<b><font color="#924517">先将所有方法放入数组中,然后循环数组</font></b>进行处理<br><br>var methods=['get','post','put','delete']<br>methods.forEach((method)=>{<br> app[method]=function(){<br> route[method].call(this)<br> }<br>})<br>
参考
https://study.163.com/course/courseLearn.htm?courseId=1209772816#/learn/live?lessonId=1280423552&courseId=1209772816
https://segmentfault.com/a/1190000002525792
0 条评论
下一页