一、编程语言
1、机器语言
2、汇编语言
3、高级语言
二、C/C++
C:“中级语言”过程式语言代表。
- 可对位,字节,地址直接操作代码和数据分离倡导结构化编程
- 功能齐全:数据类型和控制逻辑多样化
- 可移植能力强
- C语言发明人:Thompson/Ritchie
C++:面向对象语言代表
- C with Classes
- 继承
- 权限控制虚函数
- 多态
三、Lisp
Lisp:函数式语言代表
- 与机器无关
- 列表:代码即数据
- 闭包
四、JavaScript
基于原型和头等函数的多范式语言
- 过程式
- 面向对象
- 函数式
- 响应式
五、面向过程
面向过程问题
- 数据与算法关联弱
- 不利于修改和扩充
- 不利于代码重用
面向对象编程语言的问题在于,它总是附带着所有它需要的隐含环境。你想要一个香蕉,但得到的却是一个大猩猩拿着香蕉,而且还有整个丛林。————Joe Armstrong(Erlang创始人)
面向对象编程
- 封装
关联数据与算法
- 继承
无需重写的情况下进行功能扩充
- 多态
不同的结构可以进行接口共享,进而达到函数复用
- 依赖注入
面向对象编程_五大原则
- 单一职责原则SRP(Single Responsibility Principle)
- 开放封闭原则OCP(Open-Close Principle)
- 里式替换原则LSP(the Liskov Substitution Principle LSP)
- 依赖倒置原则DIP(the Dependency Inversion Principle DIP)
- 接口分离原则ISP(the Interface Segregation PrincipleISP)
函数式编程
- 函数是“第一等公民”
- 纯函数/无副作用
- 高阶函数/闭包
函数式编程_First Class Function
const BlogController = {
index(posts) { return Views.index(posts); },
show(post) { return Views.show(post);},
create(attrs) { return Db.create(attrs); },
update(post, attrs) { return Db.update(post, attrs); },
destroy(post) { return Db.destroy(mst); },};
聚合转发
const BlogController ={
index: views.index,
show: Views.show,
create: Db.create,
update: Db.update,
destroy: Db.destroy,
};
函数式编程_Pure Function
- 优势
- 可缓存
- 可移植
- 可测试
- 可推理
- 可并行
const retireAge =60;
function retirePerson(p){
if (p.age > retireAge){
p.status ='retired';}
}
function retirePerson(p){
const retireAge=60;
if (p.age > retireAge) {
return {
...p,
status: 'retired',
};
}
return p;
}
函数式编程_Currying
1、
function curry(fn) {
const arity = fn.length; 闭包
return function $curry(...args){
if (args.length <arity){
return $curry.bind(null, ...args);
}
return fn.call(null, ...args);
};
}
2、
function add(a, b,c) {
return a + b + c;
}
add(1,2,5)
add(1,2,4); 参数重复
add(1,2,7);
3、
const add_= curry(add);
const add12 = add_(1, 2);
add12(5);
add12(4); 之前参数存入闭包
add12(7);
const add1 = add_(1);
add1(2,5);
add1(3,5);
函数式编程_Composition 1、
const toUpperCase = x => x.toUpperCase();
const log =x=> console.log;
function alertUppercase(str) {手动组合
log(toUpperCase(x));
2、
const compose = (...fns) => (...args) => fns. reduceRight(
(res,fn) => [fn.callinull,...res)], args
)[0];
associativity: compose(f, compose(g.h)) === compost(compose(f. g), h);
map's composition law: compose(map(f)map(g))===ap(compose(f,g));
3、
const alertUppercaseFn = compose(log,toUpperCase);
函数式编程_Functor
函数式编程_Monad
函数式编程_Applicative
六、课程总结:
- 了解不同编程范式的起源和适用场景。
- 掌握 JavaScript在不同的编程范式特别是函数式编程范式的使用
- 掌握创建领域特定语言的相关工具和模式。