Appearance
重点柯里化函数
本节目标:理解“重点柯里化函数”的核心思路,并能把它用于实际开发或面试表达。
function isType(typing, val) {
return Object.prototype.toString.call(val) == `[object ${typing}]`;
}// 实现通用的柯里化函数: 高阶函数
function curring(fn) {
// 存储每次调用的时候传入的变量
// 存储每次调用时传入的参数
const inner = (args = []) => {
return args.length >= fn.length
? fn(...args)
// 递归返回函数
: (...userArgs) => inner([...args, ...userArgs]);
}
return inner();
}// 我们的柯里化登场: 让函数变得更具体一些,反柯里化: 让函数范围变的更大一些
let util = {};
['String', 'Number', 'Boolean', 'Null', 'Undefined'].forEach(type => {
util['is' + type] = curring(isType)(type)
});
console.log(util.isString(123))简单版
`function`checkType(content,
Type)
`{`
`return`Object.prototype.toString.call(content)
`===` `` `[object ``${Type}]` } const
`flag` `=`checkType('hello',
'String'); //
function
checkType(Type)
`{`
`return` `function`(content)
`{`
`return`Object.prototype.toString.call(content)
`===` `` `[object ``${Type}]`
} } const
`flag` `=`checkType('hello',
'String'); const
`util` `=`{}; const
`types` `=`['String',
'Number',
'Boolean']; types.forEach(type
`=>` `{`util['is'
`+`type]
`=`checkType(type); });
