Appearance
函数柯里化
函数的参数的预警 将函数的参数 进行一个保留(闭包) 闭包就是函数定义的作用域和执行作用域不是同一个 此时就是产生闭包
函数的柯里化 偏函数 都是基于高阶函数实现的
javascript
function a() {
// ...
return function () {};
}
let c = a();
c();
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
判断类型的常见 4 种方式
- typeof 可以判断基本类型 typeof null === 'object'
- instanceof 可以判断某个类型是否属于谁的实例
- Object.prototype.toString 需要再对象的原型中在找到方法
- constructor [].constrotor Array {}.constructor Object
javascript
function isType(typing, val) {
return Object.prototype.toString.call(val) === `[object ${typing}]`;
}
console.log(isType("Object", {}));
console.log(isType("String", "abc"));
console.log(isType("Number", 123));
1
2
3
4
5
6
7
2
3
4
5
6
7
javascript
function isType(typing) {
return function isNumber(val){
return Object.prototype.toString.call(val) === `[object ${typing}]`;
}
}
let isNumber = isType('Number')
let utils = {}
['Number','Boolean','String'].forEach(element => {
utils[`is${type}`] = isType(type)
});
console.log(utils.isNumber(123));
console.log(utils.isBoolean(123));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
isType 方法的范围比较大 => 小范围 isNumber(函数柯里化,将范围具体化 可以实现批量传入参数 通用的函数柯里化的实现) 函数反柯里化 Object.prototype.toString.call(val) => toString()
高阶函数的作用
- 可以拓展功能
- 可以对函数的参考进行预判参数