Skip to content

Instantly share code, notes, and snippets.

@SeqviriouM
Created April 9, 2014 14:52
Show Gist options
  • Save SeqviriouM/10279475 to your computer and use it in GitHub Desktop.
Save SeqviriouM/10279475 to your computer and use it in GitHub Desktop.
Функция с произвольным количеством скобок
/* Необходимо написать функция, которая работала бы в следующем случае:
var x = add(1)(5)(7)(11)(3) ...
console.log(x+10);
Должно вывести: 1+5+7+11+3+10 = 37
Кол-во скобок в функции add может быть произвольным.
*/
add.result = 0;
function add(x) {
arguments.callee.result += x; // аналогично add.result += x;
return (function(y) {
return add(y);
})
}
Function.prototype.valueOf = function() {
return add.result;
}
var x = add(1)(2)(3);
var y = x+5;
console.log("Function: " + x.valueOf());
console.log(x+10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment