You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var sum= function(x){
if(arguments[1]){
return x + arguments[1];
}else{
return function (y){
return x + y
}
}
}
console.log(sum(1)(2))//3
console.log(sum(1,2))//3
sum(1,2,3).toExponential(3);// "6.000e+0"sum(1)(2)(3).toExponential(3);// VM1727:1 Uncaught TypeError: sum(...)(...)(...).toExponential is not a function// at <anonymous>:1:14
function sumfn(){
var result=0;
function fn(...arg){
result = result + arg.reduce((a,b)=>a+b,0);
return fn;
}
fn.toString=function(){
var re=result;
result=0;
return re;
}
return fn;
}
var sum=sumfn();
sum(1,2,3);
sum(1)(2)(3);
sum(1)(2)(3,4,5);
var sum= function(x){
if(arguments[1]){
return x + arguments[1];
}else{
return function (y){
return x + y
}
}
}
console.log(sum(1)(2))//3
console.log(sum(1,2))//3
Activity
AnsonZnl commentedon Jun 12, 2019
在 JavaScript 高阶函数浅析 获取的灵感
thisisandy commentedon Jun 12, 2019
thisisandy commentedon Jun 12, 2019
类似于python元编程,修改toString 和toNumber 方法,使其既可以当做数字也可以当做函数调用
SCLeoX commentedon Jun 13, 2019
虽然我一时半会没想出什么解决方案但是你这个无限 curry 的不太行啊,除了显然的 typeof 结果不一样以外,还有这种,所以得到的值并不能放心用:
Gxmg commentedon Jun 14, 2019
作者 怎么也不最后给个答案呀
forever-z-133 commentedon Jun 14, 2019
haizhilin2013 commentedon Jun 14, 2019
@Gxmg 没有最后的答案
hn-failte commentedon Jun 25, 2019
此题考函数式编程:
const sum = (x, y) => (typeof y === "undefined") ? (y)=> x+y : x+y
console.log(sum(1,3));
console.log(sum(1)(3));
yuqingc commentedon Jul 1, 2019
Lodash _.curry()
可以看人家实现
haizhilin2013 commentedon Jul 1, 2019
@yuqingc 那你自己来实现一个呢?
chenyouf1996 commentedon Jul 26, 2019
Vi-jay commentedon Jul 31, 2019
lizheng1991 commentedon Aug 23, 2019
综合一下大家的写法,再自由发挥一下~话说怎么让代码高亮啊???
YiChongXie commentedon Sep 29, 2019
function add(){
var args = [...arguments]
var fn = function (){
args.push(...arguments)
return fn
}
fn.toString = function(){
return args.reduce((x,y) => x + y)
}
return fn
}
geng130127 commentedon Nov 20, 2019
function sum(a,b){
return b?a+b:function(c){
return a+c;
}
}
rni-l commentedon Jan 19, 2020
276378532 commentedon Feb 24, 2020
Alex-Li2018 commentedon Aug 12, 2020
laboonly commentedon Sep 10, 2020
smile-2008 commentedon Dec 7, 2020
Huauauaa commentedon May 7, 2021
xiaoqiangz commentedon Jun 9, 2022
柯里化函数
function sum() {
let args = [...arguments]
let fn = function() {
let fn_args = [...arguments]
return sum.apply(null, args.concat(fn_args))
}
fn.toString = function() {
return args.reduce((a, b) => a + b)
}
return fn
}
console.log(sum(1,2,3)+'')
dragon-sing commentedon Oct 31, 2022
panpanxuebing commentedon Dec 13, 2024