闭包
熟悉预编译、作用域、闭包
作用域、函数与函数执行、回调
function test(a,b){return a + b}这种是纯函数,不依赖外界,有输入和输出
闭包就是前端的一种集成用法,因为实际上是需要外部的数据,但是为了不被污染,就必须要有一个环状结构
es6写法
let Compute = (function () {let t = 1000class Compute {constructor() {this.a = 100}add(b) {return a + b}minus(b) {return a - b}}return Compute})()let res = new Compute()console.log(res.a, res.t); // 100 undefined 这样就会把定义的变量私有化,外面无法访问了
es5写法
let Compute = (function () {let a = 100function Compute() { }Compute.prototype.add = function (b) {return a + b}Compute.prototype.minus = function (b) {return a - b}return Compute})()let res = new Compute()console.log(res.a,, res.add(100)); // undefined 200
回调
callback -> 回应 -> 通讯的回应
trigger -> 触发 -》 时间的发生
call —> function
TRIGGER - > EVENT
