Git Product home page Git Product logo

crazy_fed's Introduction

置顶 👍

最新 🆕

Typescripts 总结 0 💬 2021-06-29 08:22:54

🏷️ : 面试, typescript

总结TypeScript在项目开发中的应用实践体会

条件类型

type B<T> = T extends string ? '1' : '2'

const a: B<s
[更多>>>](https://github.com/hsipeng/note/issues/96)
---

#### [前端知识总结](https://github.com/hsipeng/note/issues/95) <sup>0 :speech_balloon:</sup> 	 2020-08-08 08:40:10
:label: : [面试](https://github.com/hsipeng/note/labels/%E9%9D%A2%E8%AF%95), [前端](https://github.com/hsipeng/note/labels/%E5%89%8D%E7%AB%AF), [:+1:置顶](https://github.com/hsipeng/note/labels/%3A%2B1%3A%E7%BD%AE%E9%A1%B6)
自己整理出来的针对 P6 级别的前端知识体系

欢迎批评指正

地址是➡️  [http://hsipeng.github.io/fed](http://hsipeng.github.io/fed)
[更多>>>](https://github.com/hsipeng/note/issues/95)
---

#### [Redux 实现原理](https://github.com/hsipeng/note/issues/94) <sup>0 :speech_balloon:</sup> 	 2020-05-12 16:27:43
:label: : [react](https://github.com/hsipeng/note/labels/react), [redux](https://github.com/hsipeng/note/labels/redux)
```javascript
function createStore(reducer, state = null){
    let listeners = [];
    let getState = () => state;
    let subscribe = listener =>
[更多>>>](https://github.com/hsipeng/note/issues/94)
---

#### [ssl 生成 ssl 证书](https://github.com/hsipeng/note/issues/93) <sup>0 :speech_balloon:</sup> 	 2020-05-12 15:30:58
:label: : [linux](https://github.com/hsipeng/note/labels/linux), [ssl](https://github.com/hsipeng/note/labels/ssl)
## nginx 安装

sudo apt get install nginx


nginx 配置

server { listen 80 default; server_name domain; location / {

React 单元测试 0 💬 2019-09-16 08:17:22

🏷️ : react, 单元测试, jest

为何必须做单元测试

  • 单元测试对于任何 React 项目(及其他任何项目)来说都是必须的
  • 我们需要自动化的测试套件,根本目标是支持随时随地的代码调整、持续改进,从而提升团队响应力
  • 使用 TDD 开发是得到好的单元测试的唯一途径
  • 好的单元测试具备几大特征:不关注内部实现 更多>>>

分类 🗃️

词云, 点击展开详细分类

☁️ 词云 ☁️ 点击词云展开详细分类:point_down:

👍置顶 3:newspaper: - [前端知识总结](#95) 0 💬 - [网站工具Api搜集整理](#78) 0 💬 - [Flutter 学习整理](#76) 1 💬
🖼️封面 0:newspaper:
2B青年 4:newspaper: - [初爱](#23) 0 💬 - [关山月](#22) 0 💬 - [雪中歌](#21) 0 💬 - [江上有感](#18) 0 💬
axios 1:newspaper: - [基于 Promise 的 HTTP 请求客户端,axios](#13) 0 💬
babel 1:newspaper: - [react webpack babel 三剑客错误处理](#53) 0 💬
bug 0:newspaper:
centos 1:newspaper: - [ssh 权限问题](#41) 0 💬
coffeeScript 1:newspaper: - [CoffeeScript 基础知识](#8) 0 💬
component 1:newspaper: - [## Re: Zero 学习一个组件](#43) 0 💬
css 1:newspaper: - [前端开发,从草根到英雄(总结)](#12) 0 💬
Curry 1:newspaper: - [柯里化](#29) 0 💬
D3 1:newspaper: - [D3 与 React ](#63) 0 💬
Data Analysis 1:newspaper: - [Data Analysis with Python——01](#75) 0 💬
Data Analysis with Python 9:newspaper: - [Data Analysis with Python——09](#89) 1 💬 - [Data Analysis with Python——08](#88) 0 💬 - [Data Analysis with Python——07](#87) 0 💬 - [Data Analysis with Python——06](#86) 0 💬 - [Data Analysis with Python——05](#85) 0 💬 - [Data Analysis with Python——04](#84) 0 💬 - [Data Analysis with Python——03](#83) 0 💬 - [Data Analysis with Python——02](#82) 0 💬 - [Data Analysis with Python——01](#81) 0 💬
duplicate 0:newspaper:
enhancement 0:newspaper:
ES6 3:newspaper: - [ES6 数组去重](#57) 0 💬 - [ES6 知识点整理](#34) 0 💬 - [【译】ES 6 代理 (Proxy) 简介 ](#32) 0 💬
eslint 1:newspaper: - [vscode AutoSave With Prettier ](#64) 0 💬
flutter 1:newspaper: - [Flutter 学习整理](#76) 1 💬
gh-pages 1:newspaper: - [travis ci 持续集成](#26) 0 💬
git 2:newspaper: - [travis ci 持续集成](#26) 0 💬 - [git 安装与使用](#14) 0 💬
help wanted 0:newspaper:
html 1:newspaper: - [前端开发,从草根到英雄(总结)](#12) 0 💬
invalid 0:newspaper:
java 3:newspaper: - [Oracle 与 MySql 区别(笔记)](#6) 0 💬 - [java基础知识笔记(2)](#5) 0 💬 - [java基础知识笔记(1)](#4) 0 💬
javascript 11:newspaper: - [前端常用算法梳理](#77) 0 💬 - [ES6 数组去重](#57) 0 💬 - [函数式编程](#49) 0 💬 - [javascript 转换](#38) 0 💬 - [算法](#36) 0 💬 - [this的指向](#31) 0 💬 - [闭包的应用](#30) 0 💬 - [手撸一个 redux 实现](#28) 0 💬 - [javascript 精粹](#27) 0 💬 - [基于 Promise 的 HTTP 请求客户端,axios](#13) 0 💬 - [前端开发,从草根到英雄(总结)](#12) 0 💬
jest 1:newspaper: - [React 单元测试](#92) 0 💬
jsonp 1:newspaper: - [Promise Jsonp](#50) 0 💬
koa 1:newspaper: - [Koa 中间件](#60) 0 💬
linux 2:newspaper: - [ssl 生成 ssl 证书](#93) 0 💬 - [Ubuntu安装BTSync](#7) 1 💬
MiniProgram 1:newspaper: - [小程序ios 安卓兼容性](#65) 0 💬
Mybatis 1:newspaper: - [java SSM框架的搭建](#25) 0 💬
mysql 1:newspaper: - [Mysql 备忘](#45) 0 💬
nodejs 3:newspaper: - [Promise Jsonp](#50) 0 💬 - [travis ci 持续集成](#26) 0 💬 - [基于 Promise 的 HTTP 请求客户端,axios](#13) 0 💬
npm 1:newspaper: - [publish package to npmjs.com](#66) 0 💬
NumPy 1:newspaper: - [Data Analysis with Python——01](#75) 0 💬
poem 4:newspaper: - [初爱](#23) 0 💬 - [关山月](#22) 0 💬 - [雪中歌](#21) 0 💬 - [江上有感](#18) 0 💬
prettier 1:newspaper: - [vscode AutoSave With Prettier ](#64) 0 💬
promise 1:newspaper: - [基于 Promise 的 HTTP 请求客户端,axios](#13) 0 💬
python 14:newspaper: - [Data Analysis with Python——09](#89) 1 💬 - [Data Analysis with Python——08](#88) 0 💬 - [Data Analysis with Python——07](#87) 0 💬 - [Data Analysis with Python——06](#86) 0 💬 - [Data Analysis with Python——05](#85) 0 💬 - [Data Analysis with Python——04](#84) 0 💬 - [Data Analysis with Python——03](#83) 0 💬 - [Data Analysis with Python——02](#82) 0 💬 - [Data Analysis with Python——01](#81) 0 💬 - [python数据分析之性能度量](#80) 0 💬 - [python数据分析之数据缺失](#79) 0 💬 - [Data Analysis with Python——01](#75) 0 💬 - [python 环境](#74) 0 💬 - [python之pyenv版本控制](#15) 0 💬
question 0:newspaper:
Raspberry Pi 0:newspaper:
Re: Zero 2:newspaper: - [Re: Zero JSX 回调函数中的 this](#44) 0 💬 - [## Re: Zero 学习一个组件](#43) 0 💬
react 14:newspaper: - [Redux 实现原理](#94) 0 💬 - [React 单元测试](#92) 0 💬 - [D3 与 React ](#63) 0 💬 - [React 单元测试](#62) 0 💬 - [React 服务端渲染](#61) 0 💬 - [redux 写法详解](#59) 0 💬 - [react webpack babel 三剑客错误处理](#53) 0 💬 - [一个可用于生产环境的开发框架的搭建](#48) 0 💬 - [项目结构详解](#47) 0 💬 - [Re: Zero JSX 回调函数中的 this](#44) 0 💬 - [## Re: Zero 学习一个组件](#43) 0 💬 - [无状态函数式组件](#37) 0 💬 - [面试纪要](#33) 0 💬 - [手撸一个 redux 实现](#28) 0 💬
redux 3:newspaper: - [Redux 实现原理](#94) 0 💬 - [redux 写法详解](#59) 0 💬 - [手撸一个 redux 实现](#28) 0 💬
Spring 1:newspaper: - [java SSM框架的搭建](#25) 0 💬
SpringMVC 1:newspaper: - [java SSM框架的搭建](#25) 0 💬
ssh 1:newspaper: - [ssh 权限问题](#41) 0 💬
ssl 1:newspaper: - [ssl 生成 ssl 证书](#93) 0 💬
SSM 1:newspaper: - [java SSM框架的搭建](#25) 0 💬
ssr 1:newspaper: - [React 服务端渲染](#61) 0 💬
this 1:newspaper: - [this的指向](#31) 0 💬
Travis CI 1:newspaper: - [travis ci 持续集成](#26) 0 💬
typescript 2:newspaper: - [Typescripts 总结](#96) 0 💬 - [vue with typescript](#73) 0 💬
ubuntu 2:newspaper: - [Ubuntu使用dnsmasq作本地DNS缓存](#24) 0 💬 - [Ubuntu安装BTSync](#7) 1 💬
vscode 1:newspaper: - [vscode AutoSave With Prettier ](#64) 0 💬
vue 2:newspaper: - [vue with typescript](#73) 0 💬 - [vue全面介绍--全家桶(vue笔记一)](#10) 0 💬
Webpack 10:newspaper: - [webpack to umd package](#72) 0 💬 - [webpack 4 - webpack-merge to config dev prod environment quickly](#71) 0 💬 - [webpack4-HMR with webpack-dev-server](#70) 0 💬 - [webpack4-Atuo serve dist file](#69) 0 💬 - [# webpack4-command line without config file](#68) 0 💬 - [webpack4 - Basic config](#67) 0 💬 - [react webpack babel 三剑客错误处理](#53) 0 💬 - [webpack 3.5.5 文档 再读](#52) 0 💬 - [webpack 4 ](#39) 0 💬 - [Webpack 入门](#11) 0 💬
wechat 1:newspaper: - [小程序ios 安卓兼容性](#65) 0 💬
wontfix 0:newspaper:
函数式编程 1:newspaper: - [函数式编程](#49) 0 💬
前端 3:newspaper: - [前端知识总结](#95) 0 💬 - [前端常用算法梳理](#77) 0 💬 - [柯里化](#29) 0 💬
单元测试 2:newspaper: - [React 单元测试](#92) 0 💬 - [React 单元测试](#62) 0 💬
取整 1:newspaper: - [javascript 转换](#38) 0 💬
学习笔记 1:newspaper: - [javascript 精粹](#27) 0 💬
小程序 3:newspaper: - [小程序热区](#58) 0 💬 - [小程序组件及组件事件转发](#56) 0 💬 - [小程序热区](#55) 0 💬
开源 0:newspaper:
技术 0:newspaper:
数据分析 13:newspaper: - [如何炼就数据分析的思维?](#91) 0 💬 - [数据分析惯用的5种思维方法](#90) 0 💬 - [Data Analysis with Python——09](#89) 1 💬 - [Data Analysis with Python——08](#88) 0 💬 - [Data Analysis with Python——07](#87) 0 💬 - [Data Analysis with Python——06](#86) 0 💬 - [Data Analysis with Python——05](#85) 0 💬 - [Data Analysis with Python——04](#84) 0 💬 - [Data Analysis with Python——03](#83) 0 💬 - [Data Analysis with Python——02](#82) 0 💬 - [Data Analysis with Python——01](#81) 0 💬 - [python数据分析之性能度量](#80) 0 💬 - [python数据分析之数据缺失](#79) 0 💬
数据缺失 1:newspaper: - [python数据分析之数据缺失](#79) 0 💬
文艺 4:newspaper: - [初爱](#23) 0 💬 - [关山月](#22) 0 💬 - [雪中歌](#21) 0 💬 - [江上有感](#18) 0 💬
新生 1:newspaper: - [拿到公司电脑,之后要做些什么](#46) 0 💬
无状态组件 1:newspaper: - [无状态函数式组件](#37) 0 💬
日记 6:newspaper: - [对未来的思考](#20) 0 💬 - [谁说的青春无悔](#19) 0 💬 - [生活,需要一些仪式感 ](#17) 0 💬 - [2015-09-08-反思](#16) 0 💬 - [2017-05-19-总结](#3) 0 💬 - [热爱工作热爱生活](#2) 0 💬
柯里 1:newspaper: - [柯里化](#29) 0 💬
框架 1:newspaper: - [一个可用于生产环境的开发框架的搭建](#48) 0 💬
热区 1:newspaper: - [小程序热区](#55) 0 💬
电影 2:newspaper: - [无问西东](#51) 0 💬 - [姜文的一步之遥](#9) 0 💬
知识点 1:newspaper: - [ES6 知识点整理](#34) 0 💬
算法 2:newspaper: - [前端常用算法梳理](#77) 0 💬 - [算法](#36) 0 💬
翻译 1:newspaper: - [【译】ES 6 代理 (Proxy) 简介 ](#32) 0 💬
读书笔记 1:newspaper: - [生活,需要一些仪式感 ](#17) 0 💬
闭包 1:newspaper: - [闭包的应用](#30) 0 💬
面试 5:newspaper: - [Typescripts 总结](#96) 0 💬 - [前端知识总结](#95) 0 💬 - [面试纪要](#33) 0 💬 - [this的指向](#31) 0 💬 - [柯里化](#29) 0 💬
音乐 0:newspaper:
项目 1:newspaper: - [项目结构详解](#47) 0 💬

crazy_fed's People

Contributors

hsipeng avatar

Stargazers

 avatar

Watchers

 avatar  avatar

crazy_fed's Issues

html的doctype作用?严格模式和混杂模式都是什么?

一、Doctype作用是什么?

声明叫做文件类型定义(DTD),声明的作用为了告诉浏览器该文件的类型。让浏览器解析器知道应该用哪个规范来解析文档。声明必须在 HTML 文档的第一行,这并不是一个 HTML 标签。

二、严格模式与混杂模式如何区分?它们有何意义?

  • 严格模式:又称标准模式,是指浏览器按照 W3C 标准解析代码。

  • 混杂模式:又称怪异模式或兼容模式,是指浏览器用自己的方式解析代码。

  • 如何区分:浏览器解析时到底使用严格模式还是混杂模式,与网页中的 DTD 直接相关。

1、如果文档包含严格的 DOCTYPE ,那么它一般以严格模式呈现。(严格 DTD ——严格模式)
2、包含过渡 DTD 和 URI 的 DOCTYPE ,也以严格模式呈现,但有过渡 DTD 而没有 URI (统一资源标识符,就是声明最后的地址)会导致页面以混杂模式呈现。(有 URI 的过渡 DTD ——严格模式;没有 URI 的过渡 DTD ——混杂模式)
3、DOCTYPE 不存在或形式不正确会导致文档以混杂模式呈现。(DTD不存在或者格式不正确——混杂模式)
4、HTML5 没有 DTD ,因此也就没有严格模式与混杂模式的区别,HTML5 有相对宽松的语法,实现时,已经尽可能大的实现了向后兼容。( HTML5 没有严格和混杂之分)

意义:

严格模式与混杂模式存在的意义与其来源密切相关,如果说只存在严格模式,那么许多旧网站必然受到影响,如果只存在混杂模式,那么会回到当时浏览器大战时的混乱,每个浏览器都有自己的解析模式。

map/reduce练习题

不要使用JavaScript内置的parseInt()和Number()函数,利用map和reduce操作实现一个string2int()函数。
分析:把一个字符串"13579"先变成Array——[1, 3, 5, 7, 9],再利用reduce()就可以写出一个把字符串转换为Number的函数。

'use strict';

function string2int(s) {
    function str2num(str){
        var strArr = str.split('');               //把字符串分割成字符串数组
        function toInt(data){
            return +data;                  //通过js的弱类型转换,实现字符类型到数字类型的转换
        }
        var numArr = strArr.map(toInt);           //通过map()把字符串数组转换成数字数组
        return numArr;
    }
    var num = str2num(s);
    var res = num.reduce(function (x,y) {        //通过reduce()把数字数组转换成数字量
        return x*10+y;
    }); 
    return res;
}
// 测试:
if (string2int('12345') === 12345 && string2int('12300') === 12300) {
    if (string2int.toString().indexOf('parseInt') !== -1) {
        alert('请勿使用parseInt()!');
    } else if (string2int.toString().indexOf('Number') !== -1) {
        alert('请勿使用Number()!');
    } else {
        alert('测试通过!');
    }
}
else {
    alert('测试失败!');
}

注:map()是数组Array的一个方法,传入一个指定方法,返回一个由原数组中的每个元素调用一个指定方法后的返回值组成的新数组。
Array的reduce()把一个函数作用在这个Array的[x1, x2, x3...]上,这个函数必须接收两个参数,reduce()把结果继续和序列的下一个元素做累积计算,其效果就是:
[x1, x2, x3, x4].reduce(f) = f(f(f(x1, x2), x3), x4)
js中把字符类型转成数字类型的方法主要有三种:转换函数parseInt()、强制类型转换Number()、利用js变量弱类型转换。

CSS 选择符有哪些?哪些属性可以继承?优先级算法如何计算?

选择器

  • 1.id选择器( # myid)

  • 2.类选择器(.myclassname)

  • 3.标签选择器(div, h1, p)

  • 4.相邻选择器(h1 + p)

  • 5.子选择器(ul > li)

  • 6.后代选择器(li a)

  • 7.通配符选择器(*)

  • 8.属性选择器(a[rel = "external"])

  • 9.伪类选择器(a: hover, li: nth - child)

可继承的样式:

font-size font-family color, UL LI DL DD DT;

不可继承的样式:

border padding margin width height

优先级

优先级就近原则,同权重情况下样式定义最近者为准,载入样式以最后载入的定位为准;

优先级为:

!important > id > class > tag

important 比 内联优先级高

优先级数值计算

  1. 内联样式表的权值最高 1000;

  2. ID 选择器的权值为 100

  3. Class 类选择器的权值为 10

  4. HTML 标签选择器的权值为 1

CSS3新增伪类举例

p:first-of-type 选择属于其父元素的首个元素的每个元素。

p:last-of-type 选择属于其父元素的最后元素的每个元素。

p:only-of-type 选择属于其父元素唯一的元素的每个元素。

p:only-child 选择属于其父元素的唯一子元素的每个元素。

p:nth-child(2) 选择属于其父元素的第二个子元素的每个元素。

:enabled :disabled 控制表单控件的禁用状态。

:checked 单选框或复选框被选中。

请尝试用filter()筛选出素数

'use strict';

function get_primes(arr) {
return arr.filter(function(element,index,self){
       if(element == 2){
          return true;
        }
        if(element == 1){
        return false;
        }
         
        if(element%2 == 0){
        return false;
        }
        
         for(var i=3;i<(element/2+1);i=i+2){
              if(element%i == 0){
                return false; 
                }
           
         }
     return true;

});
}

// 测试:
var
    x,
    r,
    arr = [];
for (x = 1; x < 100; x++) {
    arr.push(x);
}
r = get_primes(arr);
if (r.toString() === [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97].toString()) {
    alert('测试通过!');
} else {
    alert('测试失败: ' + r.toString());
}

node js

Date.now();

async await
需要babel 转码:

If you just use in development. You can do this:

npm i babel-cli babel-plugin-transform-async-to-generator babel-polyfill --save-dev

the package.json would be like this:

"devDependencies": {
   "babel-cli": "^6.18.0",
   "babel-plugin-transform-async-to-generator": "^6.16.0",
   "babel-polyfill": "^6.20.0"
}

create .babelrc file and write this:

{
  "plugins": ["transform-async-to-generator"]
}

and then, run your async/await script like this:

./node_modules/.bin/babel-node script.js

class_a .class_b(中间有空格) 和 .class_a.class_b 和.class_a,.class_b三者区别是什么?

.example .pp = E F 是后代选择器。
.example.pp2 则是在一个元素上,这个元素包括这两个类才会有效果。
分四种情形:
一:#a,b{…………} id叫a和一个标签是b的样式
二:#a b{…………} id叫a下面的一个标签是b的样式
三:#a:b{…………} id叫a的伪类b的样式(如a:link{background:#00CCFF} a:hover{background:#FF0000})
四:#a.b{…………} id叫a的下面的class叫b的样式

express 传参数

总结出以下4点:
1、对于path中的变量,均可以使用req.params.xxxxx方法
2、对于get请求的?xxxx=,使用req.query.xxxxx方法
3、对于post请求中的变量,使用req.body.xxxxx方法
4、以上三种情形,均可以使用req.param()方法,所以说req.param()是req.query、req.body、以及req.params获取参数的三种方式的封装。

setTimeout()的时间参数最小可以是多少?为什么?

根据HTML5标准,setTimeOut推迟执行的时间,最少是4毫秒。如果小于这个值,会被自动增加到4。这是为了防止多个setTimeout(f,0)语句连续执行,造成性能问题。
另一方面,浏览器内部使用32位带符号的整数,来储存推迟执行的时间。这意味着setTimeout最多只能推迟执行2147483647毫秒(24.8天),超过这个时间会发生溢出,导致回调函数将在当前任务队列结束后立即执行,即等同于setTimeout(f,0)的效果。

js异步编程的方法

PS:
avascript语言的执行环境是"单线程"(single thread)。
所谓"单线程",就是指一次只能完成一件任务。如果有多个任务,就必须排队,前面一个任务完成,再执行后面一个任务,以此类推。

一、回调函数(同步操作变异步操作)

这是异步编程最基本的方法。
假定有两个函数f1和f2,后者等待前者的执行结果。

如果f1是一个很耗时的任务,可以考虑改写f1,把f2写成f1的回调函数。

function f1(callback){
    setTimeout(function () {
      // f1的任务代码
      callback();
    }, 1000);
  }

执行代码就变成下面这样:

  f1(f2);

二、事件监听

另一种思路是采用事件驱动模式。任务的执行不取决于代码的顺序,而取决于某个事件是否发生。
还是以f1和f2为例。首先,为f1绑定一个事件(这里采用的jQuery的写法)。

f1.on('done', f2);

 
上面这行代码的意思是,当f1发生done事件,就执行f2。然后,对f1进行改写:

  function f1(){
    setTimeout(function () {
      // f1的任务代码
      f1.trigger('done');
    }, 1000);
  }

f1.trigger('done')表示,执行完成后,立即触发done事件,从而开始执行f2。

三、发布/订阅

上一节的"事件",完全可以理解成"信号"。
我们假定,存在一个"信号中心",某个任务执行完成,就向信号中心"发布"(publish)一个信号,其他任务可以向信号中心"订阅"(subscribe)这个信号,从而知道什么时候自己可以开始执行。这就叫做"发布/订阅模式"(publish-subscribe pattern),又称"观察者模式"(observer pattern)。

这个模式有多种实现,下面采用的是Ben Alman的Tiny Pub/Sub,这是jQuery的一个插件。
首先,f2向"信号中心"jQuery订阅"done"信号。

  jQuery.subscribe("done", f2);

然后,f1进行如下改写:

  function f1(){
    setTimeout(function () {
      // f1的任务代码
      jQuery.publish("done");
    }, 1000);
  }

jQuery.publish("done")的意思是,f1执行完成后,向"信号中心"jQuery发布"done"信号,从而引发f2的执行。
此外,f2完成执行后,也可以取消订阅(unsubscribe)。

  jQuery.unsubscribe("done", f2);

四、Promises对象

Promises对象是CommonJS工作组提出的一种规范,目的是为异步编程提供统一接口。
简单说,它的**是,每一个异步任务返回一个Promise对象,该对象有一个then方法,允许指定回调函数。比如,f1的回调函数f2,可以写成:

  f1().then(f2);

f1要进行如下改写(这里使用的是jQuery的实现):

  function f1(){
    var dfd = $.Deferred();
    setTimeout(function () {
      // f1的任务代码
      dfd.resolve();
    }, 500);
    return dfd.promise;
  }

这样写的优点在于,回调函数变成了链式写法,程序的流程可以看得很清楚,而且有一整套的配套方法,可以实现许多强大的功能。
比如,指定多个回调函数:

  f1().then(f2).then(f3);

再比如,指定发生错误时的回调函数:

  f1().then(f2).fail(f3);

而且,它还有一个前面三种方法都没有的好处:如果一个任务已经完成,再添加回调函数,该回调函数会立即执行。所以,你不用担心是否错过了某个事件或信号。这种方法的缺点就是编写和理解,都相对比较难。

知识点Link

Asynchronous JS

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.