Сообщение от Mess4me
|
tsigel,
а без valueOf?
|
а разве это было в задании ?
function sum() {
var currentSum = getSum(arguments);
function fn() {
currentSum += getSum(arguments);
return fn;
}
function getSum(args) {
var argSum = 0;
for (var i = 0; i <args.length; i++) {
argSum += args[i]
};
return argSum;
}
fn.toString = fn.valueOf = function() {
return currentSum;
};
return fn;
}
alert( 'sum(2, 5, 10) = ' + sum(2, 5, 10) + "\n"
+ 'sum(2, 5)(10) = ' + sum(2, 5)(10) + "\n"
+ 'sum(2)(5)(10) = ' + sum(2)(5)(10) + "\n"
+ 'sum(2)(5, 10) = ' + sum(2)(5, 10));
Если количество вызываемых аргументов равно 3
function sum() {
var currentSum = getSum(arguments);
function fn() {
currentSum += getSum(arguments);
return checked(arguments.length);
}
function getSum(args) {
var argSum = 0;
for (var i = 0; i < args.length; i++) {
argSum += args[i]
}
return argSum;
}
function checked(col) {
fn.arg = fn.arg || 0;
fn.arg += col;
return fn.arg == 3 ? currentSum : fn;
}
return checked(arguments.length);
}
alert( 'sum(2, 5, 10) = ' + sum(2, 5, 10) + "\n"
+ 'sum(2, 5)(10) = ' + sum(2, 5)(10) + "\n"
+ 'sum(2)(5)(10) = ' + sum(2)(5)(10) + "\n"
+ 'sum(2)(5, 10) = ' + sum(2)(5, 10));