什么是偏函数
偏函数是函数柯里化的其中一种表现,固化之前函数的一部分参数,将其设置为默认值,
然后返回一个新的函数。
怎么使用偏函数
我们需要将原本函数中的的一部分参数设置成默认值。使用一个函数返回一个函数,并将这个默认值作为return出来的函数的函数体内容被使用。当我们下次调用这个外层函数时,给这个默认值赋值。
场景1判断变量类型
const isType = function(type, variable) {
return Object.prototype.toString.call(variable) === "[object " + type + "]";
};
console.log(isType("Object", {}));
const isTypeCurrying = function(type) {
return function(variable) {
return Object.prototype.toString.call(variable) === "[object " + type + "]";
}
}
const isTypeCurryingFunction = isTypeCurrying("Function");
console.log(isTypeCurryingFunction(function() {}));
场景2动态生成一个标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const createTags = function(tagType, content) {
const startTag = "<" + tagType + ">";
const endTag = "</" + tagType + ">";
return `${startTag + content + endTag}`;
};
document.write(createTags("h1", "标题"));
const createTagsCurrying = function(tagType) {
const startTag = "<" + tagType + ">";
const endTag = "</" + tagType + ">";
return function(content) {
return `${startTag + content + endTag}`;
}
}
const createDiv = createTagsCurrying("div");
document.write(createDiv("我是一个div"));
const createP = createTagsCurrying("p");
document.write(createDiv("我是一个p"));
</script>
</body>
</html>
![在这里插入图片描述](https://img-blog.csdnimg.cn/87336f947f9d4ee8bf14e9d1c0920bb6.png)
为什么要用偏函数
不知道大家看见上面两个案例有没有发现,两个案例的原始函数涉及的业务,换句话说,
实现的功能是比较大的。专业一点的讲:就是代码耦合度过大,原始函数使用起来需要
考虑的事情比较多。但是我们使用偏函数就可以将业务拆分成小业务,判断所有类型到
判断是否是函数,生成所有类型的标签到生成div标签。不管是使用起来还是后续维护起
来都会方便很多。(单单是看函数命名就能感觉到代码和事情变简单了不是)
复杂场景感受一下偏函数的好处
function select(type, num) {
if (type == 'a') {
console.log(num + 1);
} else if (type == 'b') {
console.log(num * 1);
} else if (type == 'c') {
console.log(num ** 2);
} else if (type == 'd') {
console.log(num / 2);
}
}
select("a", 10);
select("b", 10);
select("c", 10);
select("d", 10);
现在领导让你在这个功能里面加一段业务,你可能会这么做。
function select(type, num) {
if (type == 'a') {
console.log(num + 1);
} else if (type == 'b') {
console.log(num * 1);
} else if (type == 'c') {
console.log(num ** 2);
} else if (type == 'd') {
console.log(num / 2);
} else if (type == 'e') {
console.log("新加的功能" + num % 2);
}
}
select("a", 10);
select("b", 10);
select("c", 10);
select("d", 10);
select("e", 10);
这不是实现了,这不挺好的。but,现在是五个业务,看起来还行。但是业务多起来之后怎么
办每次调用select都会走一次循环语句,循环语句是会消耗性能的。咱们每次调用我们自己
的业务都要去跑一次循环语句?打开联机版GTA 5一条if语句跑了19.8亿次及其占用cpu性能。
导致打开时间过长这个事大家听说过没有?
如果使用偏函数会怎么样呢?
function select(type, num) {
if (type == 'a') {
return function() {
console.log(num + 1);
}
} else if (type == 'b') {
return function() {
console.log(num * 1);
}
} else if (type == 'c') {
return function() {
console.log(num ** 2);
}
} else if (type == 'd') {
return function() {
console.log(num / 2);
}
} else if (type == 'e') {
console.log("走循环了");
return function() {
console.log("新加的功能" + num % 2);
}
}
}
const selectA = select("a", 10);
const selectB = select("b", 10);
const selectC = select("c", 10);
const selectD = select("d", 10);
const selectE = select("e", 10);
selectB();
selectC();
selectD();
selectE();
selectE();
selectE();
selectE();
selectE();
![在这里插入图片描述](https://img-blog.csdnimg.cn/c2154918c474405bac8ef6257c0908c4.png)
不管我们调用了多少次我们写的功能函数,都只会走一次循环。Salute to you这个知识点
讲完啦。不知道大家有没有学会呢?