【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
利用了闭包,保存了对各个方法参数的引用,虽然方法出栈了,但是变量对象还保存在内存中
计算体积
function volume(h) {
return (h) => {
return (l) => {
return (w) => {
return h * l w;
}
}
}
}
var result = volume(10)(20)(30);
//如果说想计算固定高度的volume
function volume100() {
}
var volume100 = volume(100);
//不用每次都传相同的参数100
var result1 = volume100(10)(20);
var result2 = volume100(40)(50);
计算折扣
function discount(discount) {
return (price) => {
return price * discount;
}
}
const tenPercentDiscount = discount(0.1)
const twentyPercentDiscount = discount(0.2);
来源:oschina
链接:https://my.oschina.net/u/2285087/blog/3155798