Git Product home page Git Product logo

hsipeng.github.io'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 💬

hsipeng.github.io's People

Contributors

hsipeng avatar

Forkers

goldsixchen

hsipeng.github.io's Issues

javascript 精粹

javascript 精粹

方法定义函数

Funtction.prototype.method = function (name, func) {
	this.prototype[name] = func;
	return this;
}

语法

  • 空白(space)

注释用// 避免一些错误
例如

/*
	var rm_a = /a*/.match(s)
*/
  • 标识符

字母开头 加上字母,数字,下划线

  • 数字

JavaScript 只有一个数字类型 为64位浮点数
NaN 表示不能产生正常的运算结果, 可以用isNaN(number)检测NaN
Infinity 表示 大于 1.7969313482315704+308的值

  • 字符串

\ 反斜杠 是转义字符
所有字符都是Unicode (16位字符集)

  • 语句

当做假(falsy)

false
null
undefined
空字符串 ‘ ’
数字 0
数字 NaN

for in 语句 枚举一个对象的所有属性

  • 表达式

运算符优先级

运算符 说明
. [] () 提取属性与调用函数
delete new typeof + - 一元运算符
* / % 乘法 除法 求余
+ - 加法/链接 减法
>= <= > < 不等式运算
=== !=== 等式运算符
&& 逻辑与
| | 逻辑或
? : 三元
  • 字面量

创建新对象的表示法

  • 函数

函数字面量定义函数值,可以有一个可选的名字,用于递归调用自己

对象

JavaScript 简单数据类型 数字、字符串、布尔值、null值和undefined
其他所有值都是对象。

  • 对象字面量

一个对象字面就是包围再一堆花括号中的零或多个“名/值"对。

	var empty_object = {};
	var stooge = {
		"first-name": "Jerome",
		"last-name": "Howard"
	}
  • 检索

检索对象里包含的值
[] 后缀中括住一个字符串表达式的方式

	stooge["first-name"]
	flight.departure.IATA
  • 更新

对象里的值可以通过赋值语句来更新
如果不存在会扩充到该对象中

stooge['middle-name'] = 'Lester'
  • 引用

对象通过引用来传递,它们永远不会被复制

	var x = stooge;
	x.nickname = 'Curly';
	var nick = stooge.nickname;
  • 原型

每个对象都链接到一个原型对象,并且它可以从中继承属性。
所有通过字面量对象创建的对象都连接到 Object.prototype,它是JavaScript标配对象

if(typeof Object.beget !== 'function') {
	Object.create = function (o) {
		var F = function () {};
		F.prototype = o;
		return new F();
	}
}
  • 反射

typeof 确定对象的属性

typeof flight.toString // 'function'
typeof flight.constructor //'function'

hasOwnPerty 不检查原型链 如果对象拥有独有的属性 返回true

flight.hasOwnProperty('number') // true
flight.hasOwnProperty('constructor')    //false
  • 枚举

for in 遍历一个对象的所有属性 用hasOwnProperty 或者typeof 来排除函数

var name;
for (name in anoter_stooge) {
	if (typeof anoter_stooge[name] !== 'function') {
		document.writeln( name + ': ' + anoter_stooge[name]);
	}
}
  • 删除

delete 运算符可以用来删除对象的属性。如果对象包含该属性,那么该属性就会被移除。它不影响原型链中的任何属性。

another_stooge.nickname  // 'Moe'

//删除 anoter_stooge 的nickname 属性, 从而暴露出原型的nickname属性
delete anoter_stooge.nickname

another_stooge.nickname   // ‘Curly'
  • 减少全局变量污染

全局变量削弱了JavaScript的灵活性,应该避免使用。
最小化使用全局变量的方法之一是为你的应用只创建一个唯一的全局变量
还有就是应用闭包隐藏信息,从而减少全局污染

var MYAPP = {};

MYAPP.stooge = {
	"first-name": "Joe",
	"last-name": "Howard"
};

MYAPP.flight = {
	airline: "Oceanic",
	number: 815,
	departure: {
		IATA: "SYD",
		time: "2014-09--22 14:45",
		city: "Sydney"
	},
	arrival: {
		IATA: "LAX"
		time: "2004-09-23",
		city: "Los angeles"
	}

};
...

函数

  • 函数对象

JavaScript 中的函数就是对象。
对象的字面量产生的对象连接到Object.prototype
函数对象连接到Function.prototype(该原型对象本身连接到Object.prototype)

函数创建时拥有两个隐藏属性

- 函数上下文
- 实现函数行为的代码

函数对象的prototype 属性的值拥有constructor 属性且值位该函数的对象

  • 函数字面量
//创建一个名为add的变量,并用来吧两个数字相加的函数赋值给它

var add = function (a, b) {
	return a+b;
};

包括四个部分

1. 保留字 function 
2. 函数名  它可以省略 ,省略时 即为 匿名函数(anonymous)
3. 圆括号内的 一组参数
4. 花括号的一组语句  函数的主体

函数也可以被定义再其他函数中,一个内部函数处理可以访问自己的参数和变量,同时也能访问把它嵌套再其中的父函数的参数和变量。通过函数字面量创建的函数对象包含一个链接到外部上下文的链接。这称为闭包(closure).

  • 调用

调用一个函数会暂停当前函数的执行,传递控制权和参数给新函数。
除了声明时定义的参数外,每个函数还会接收两个附加的参数 : this 和 arguments。

一共四种调用模式
(初始化关键参数 this 的不同)

1. 方法调用模式
2. 函数调用模式
3. 构造器调用模式
4.apply调用模式

方法调用模式

当一个函数被保存为对象的一个属性时,我们称它位一个方法。
当一个方法被调用时,this 被绑定到该对象。
如果调用表达式包含一个提取属性的动作(即包含一个. 表达式或者 [subscrript]下标表达式),那么它就是被当做一个方法来调用。

// 创建一个myObject 对象 它有一个value 属性和一个increment 方法
// increment 方法接收一个可选的参数。 如果参数不是数字, 那么默认使用数字1

var myObject = {
	value: 0,
	increment: function (inc) {
		this.value += typeof inc === 'undefined' ? inc : 1;
	}
};

myObject.increment();
document.writlen(myObject.value); // 1

myObject.increment(2);
document.writlen(myObject.value);   // 3

函数调用

var sum = add (3,4);

当一个函数并非一个对象的属性时,那么它就是被当做一个函数来调用
this 绑定到全局对象。这是一个语言设计上的错误。
解决方法:

myObject.double = function () {
	var that = this;
	var helper = function () {
		that.value = add(that.value, that.value);
	}
	helper();// 以函数的形式调用helper
};

// 以方法的形式调用 double
myObject.double();
document.writeln(myObject.value);

构造器调用模式

JavaScript 是一门基于原型继承的语言。
一个函数前面带上new 来调用,那么背地里将会创建一个连接到该函数的prototype成员的新对象 ,this 绑定到那个新对象上。

// 创建一个名为 Quo 的构造器函数。

var Quo = function (String) {
	this.status = String;
};

// Quo 的所有实例提供一个名为get_status 的公共方法

Quo.prototype.get_status = function () {
	return this.satus;
};

// 构造一个Quo 实例。
var myQuo = new Quo("confused");
document.writeln(myQue.get_status()):    // "confused"

Apply 调用模式

因为JavaScript 是一门函数式的面向对象编程语言,所以函数可以拥有方法
apply 方法让我们构建一个参数数组传递给调用函数。它允许我们选择this的值。
apply 接收连个参数,一个是要绑定this的值, 第二个是一个参数数组

// 构造一个包含两个数字的数组,并把他们两个相加

var array = [3, 4];
var sum = add.apply(null, array); // sum 值为7

// 构造包含status 成员的对象

var statusObject = {
	status: 'A-OK'
};

// statusObject  并没有继承自Que.prototype,但我们可以再statusObject 上
// 调用get_status 方法,尽管statusObject 并没有一个名为get_status 方法

var status = Quo.prototype.get_status.apply(statusObject);
// status 的值为 'A-OK'
  • 参数

函数调用时会默认有一个 arguments 参数,arguments 可以访问被调用时传递给它的参数列表。

var sum = function () {
	var i,sum = 0;
	for ( i = 0; i < arguments.length; i += 1) {
		sum += arguments[i];
	}
	return sum;
};

document.writeln(sum(4, 8, 15, 16, 23, 42)); // 108
  • 返回

return 语句 用来提前返回

  • 异常

异常是干扰程序的正常流程的不寻常的事故(但并非出乎意料)

var add = function (a, b) {
	if (typeof a !== 'number' || typeof b !== 'number') {
		throw {
			name: 'TypeError',
			message: 'add needs numbers'
		};
		return a + b;
	}
}

throw 语句中断函数的执行,它应该抛出一个exception对象
该exception 对象将被传递到一个try 语句的catch 从句:

// 构造一个try_it 函数 以不正确的方式调用之前的add 函数
var try_it = function () {
	try {
		add("seven");
	} catch (e){
		document.writeln(e.name + ': ' + e.message);
	}
}
  • 扩充类型的功能

JavaScript 允许给语言的基本类型扩充功能。
扩充 Function.prototype 加入method方法,方便添加方法

Funtction.prototype.method = function (name, func) {
	this.prototype[name] = func;
	return this;
}

比如给Number.prototype 增加一个integer方法

Number.method('integer', function () {
	return Math[this < 0 ? 'ceil' : 'floor' ] (this);
})

document.writeln((-10 / 3).integer());  // -3 

不存在时再添加。

Funtction.prototype.method = function (name, func) {
	if (! this.prototype[name]) {
		this.prototype[name] = func;
	}
	return this;
}
  • 递归

递归函数就是会直接或间接的调用自身的一种函数。

汉诺塔

var hanoi = function (disc, src, aux, dst) {
	if (disc > 0) {
		hanoi(disc - 1, src, dst, aux);
		document.writeln('Move disc ' + disc + ' from ' + src + 'to ' + dst);
		hanoi(disc - 1, aux, src, dst);
	}
}
  • 作用域

作用域控制着变量与参数的可见性以及生命周期

  • 闭包

作用域的好处是内部函数可以访问定义他们的外部函数的的参数和变量(除了this和arguments)
函数可以访问它被创建时所处的上下文环境,所以才叫闭包
保护value不被非法更改

var myObject = (function () {
	var value = 0;
	return {
		increment: function (inc) {
			value += typeof inc === 'number' ? inc : 1;
		},
		getValue: function () {
			return value;
		}
	};
}());

定义一个函数,它设置一个DOM节点为黄色,然后把它渐变为白色

var fade = function (node) {
	var level = 1;
	var step = function () {
		var hex = level.toString(16);
		node.style.backgroundColor = '#FFFF' + hex + hex;
		if (level < 15) {
			level += 1;
			setTimeout(step, 100);
		}
	};
	setTimeout(step, 100);
};

fade(document.body);

避免再循环中创建函数,他可能只会带来无谓的基三,还会引起混淆。
我们可以现在循环外创建一个辅助函数,让这个负主函数再返回绑定了当前i的函数

var add_the_handlers =function (nodes) {
	var helper = function (i) {
		return function (e) {
			alert(i);
		};
	};
	var i;
	for (i = 0; i < nodes.length; i += 1) {
		nodes[i].onclick = helper(i);
	}
};
  • 回调

正常模拟服务器响应

request = prepare_the_request();
response = send_request_synchronously(request);
display(response );

这是最自然的写法,但是如果网络上的同步请求会导致客户端进入假死状态。
所以应用异步函数,防止阻塞

request = prepare_the_request();
response = send_request_synchronously(request, function (response){
	display(request);
});
  • 模块

我们可以使用函数和闭包来构造模块

给String 增加一个deentityify 方法, 理想方式是把它放入闭包:

String.method('deentityify', function () {
	// 字符实体表 银蛇字符实体名字到对应的字符
	var entity = {
	quot: '"',
	lt:  '<',
	gt: '>'
	};

	return function () {
	// 这是真正的deenityify 方法。 调用replace
		return this.replace(/&([^&;]+);/g,
			function(a, b) {
				var r = entity[b];
				return typeof r === 'string' ? r : a;
			}
		);
	};
}());
  • 级联

一些方法返回this而不是undefined 就可以启用级联。
在一个级联中,我们可以单独一条语句一次调用同一个对象的很多方法。
Ajax 类库调用例子:

getElement('myBoxDiv')
	.move(350, 150)
	.width(100)
	.height(100);
  • 柯里化(curry)

函数也是值,从而我们可以用有趣的方式去操作函数值。柯里化允许我们把函数传递给它的参数相结合,产生一个新的函数。

var add1 = add.curry(1);
document.writeln(add1(6));   // 7

创建curry需要注意

arguments 数组并非一个真正的数组,所以并没有concat方法。要避开这个问题,我们必须要再两个argumengts数组上都应用slice方法

Function.method('curry', function () {
	var slice = Array.prototype.slice,
	args = slice.appley(arguments),
	that = this;
	return function () {
		return that.apply(null,args.concat(slice.apply(arguments)));
	}
});
  • 记忆

函数可以将先前的操作的结果记录在某个对象里,从而避免无谓的重复运算。

以Fibonacci 为列

var fibonacci  = function (n) {
	return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2 );
}

for (var i = 0; i <= 10; i += 1) {
	document.writeln('//' + i + ': ' + fibonacci(i));
}

上面fibonacci函数被调用了453次,我们调用了 11次。它调用了吱声442次去计算。

改进加入记忆:

var fibonacci = function () {
	var memo = [0,1];
	var fib = function (n) {
		var result = memo[n];
		if (typeof result !== 'number') {
			result = fib(n - 1) + fib (n - 2);
			memo[n] = result;
		}
		return result;
	};
return fib;
}();

fibonacci函数只被调用了29次,我们调用了 11次。它调用了18次去取得之前的存储结果。

推广记忆功能

var memoizer = function (memo, formula) {
	var recur = function (n) {
		var result = memo[n];
		if (typeof result !== 'number') {
			result = formula (recur, n);
			memo[n] = result;
		}
		return result;
	};
	return recur;
};

那么fibonacci 函数就可以这样写

var fibonacci = memoizer([0,1], function (recur, n) {
	return recur (n -1) + recur (n -2);
});

带记忆功能的阶乘函数

var factorial = memoizer ([1,1], function (recur, n) {
	return n*recur(n - 1);
});

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.