1

请教 柯里化 的作用

 1 year ago
source link: https://www.v2ex.com/t/884884
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  问与答

请教 柯里化 的作用

  Znella · 2 小时 50 分钟前 · 551 次点击

起因:函数式编程的讨论,看到众多大佬讨论函数式编程的优劣。忽然想到自己最近看 axios 源码中的例子,在判断类型时用了柯里化的技巧。


const kindOf = (cache => thing => {

	const str = toString.call(thing);

	return cache[str] || (cache[str] = str.slice(8, -1).toLowerCase());

})(Object.create(null));

  

const kindOfTest = (type) => {

	type = type.toLowerCase();

	return (thing) => kindOf(thing) === type

}

const isDate = kindOfTest('Date');

想请问大家,这样的技巧有什么 具体 的作用呢?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK