1.js面向过程
let counter = 1
counter = counter + 2
counter = counter + 3
counter = counter + 4
counter = counter + 5
console.log(counter ) //输出为15
2.面向对象
const counter = {
value: null,
init(v) {
this.value = v
},
inc(v) {
this.value = this.value + v
}
}
counter.init(1)
counter.inc(2)
counter.inc(3)
counter.inc(4)
counter.inc(5)
console.log(counter.value) // 输出15
3.面向函数(利用闭包实现加法)
//加法运算
const useInc = v => nv => v = v + nv
//翻译代码
//function useInc (v){
// return function(nv){
// return v=v+nv
// }
// }
const inc = useInc(1)
inc(2)
inc(3)
inc(4)
const counter = inc(5)
console.log(counter)