Created
April 9, 2014 14:52
-
-
Save SeqviriouM/10279475 to your computer and use it in GitHub Desktop.
Функция с произвольным количеством скобок
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Необходимо написать функция, которая работала бы в следующем случае: | |
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