5 ms·
Yep, that is exactly the solution I would be looking for. I would be blown a way if someone came up with solution that could handle arbitrary function returns.
by Xavi 16y ago
Yep, that is exactly the solution I would be looking for.
I would be blown a way if someone came up with solution that could handle arbitrary function returns. My friend actually wrote an interesting article on the subject. Here's the link if you're interested: http://msdn.microsoft.com/en-US/scriptjunkie/gg575560.aspx http://msdn.microsoft.com/en-US/scriptjunkie/gg575560.aspx
- jaysoo 16y agoNot exactly the same signature, but can handle arbitrary number of calls. var say = (function() { var savedArgs = []; return (function recurse () { if (arguments.length) { for (var i=0, arg; arg=arguments[i]; i++) { savedArgs.push(arg); } return recurse; } else { alert(savedArgs.join(' ')); savedArgs = []; } }); })(); // e.g. say('Hello')('World')('!!')(); // --> alerts "Hello World !!"
- sibsibsib 16y agohere's the version I came up with, based on one I did in python. It's very similar to jaysoo's - needs a call with no args to terminate the list. I'm curious if there's a way to do this somehow without the terminator. I'm not a JS expert by any means, so if I'm doing something stupid in this code, I'd love to know. var say = function(word) { words = [word]; func = function(word2) { if (word2) { words.push(word2); return func; } else { print(words.join(' ')); } } return func; }; say('hello')('bob')('how you doing?')(); *note I have the function 'print' aliased to console.log for convenience.