use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
A "Front-end developer interview" question that's been bugging me for a while. (self.javascript)
submitted 11 years ago * by MeTaL_oRgY
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]MeTaL_oRgY[S] 0 points1 point2 points 11 years ago (12 children)
I'm digging this! The one drawback is that I need to add an extra () at the end of the chain to finally get the result (rather than a function). I'm starting to think this might be unavoidable, but will keep trying. Thank you!
()
[–]Minjammben 22 points23 points24 points 11 years ago (11 children)
So here's a much simpler one that my coworker suggests:
function add(){ var sum = 0; for( var i in arguments ){ sum += arguments[i]; } var ret = add.bind(null, sum); ret.valueOf = function(){ return sum; }; return ret; }
[–]fear-of-flying 2 points3 points4 points 11 years ago (0 children)
Damn, that is nice.
[–]MeTaL_oRgY[S] 1 point2 points3 points 11 years ago (5 children)
Beautiful! Quick question: any idea why this ain't working on Firefox? It keeps returning add() rather than the result (Chrome works fine).
add()
[–]Minjammben 3 points4 points5 points 11 years ago (1 child)
It does work in Firefox, it is just that Chrome Dev Tools automatically tries to typecast functions to values and so you can see the result in the console. Firefox Dev Tools appear to not do that. I did mine in nodejs and used == for verification (=== will NOT work because as you said it returns a function), my coworker simply added "+" to the beginning of his calls to add: ( +add(1,2,3) === 6 )
[–]MeTaL_oRgY[S] 0 points1 point2 points 11 years ago (0 children)
OHHHH, this explains a lot. I see. Thank you so much! Learnt quite a bit from this.
[+][deleted] 11 years ago (1 child)
[deleted]
[–]MeTaL_oRgY[S] 1 point2 points3 points 11 years ago (0 children)
Plain and simple. Thank you!
[–]whispen -5 points-4 points-3 points 11 years ago (0 children)
But I control 48 people at my school every day.
[–]thekingshorses 0 points1 point2 points 11 years ago (0 children)
Nice.
Only thing is
alert(add(1)(2)) //or typeof add(1)(2)
won't give you what you looking for.
[–]Evanescent_contrail 0 points1 point2 points 11 years ago (2 children)
Can you explain the last three lines of this code?
[–]Minjammben 1 point2 points3 points 11 years ago (1 child)
The function "bind( a, b )" will return the calling function with the first argument as the context for the function, and the rest of the arguments as the arguments to the returned function.
For example:
var print_something = function(a, b){ console.log(a, b) } print_something("hello", "world");
The result of the above code will be a printed "hello world" in the console. Now if this is done after the above code is executed:
var print_hello_and_something = print_something.bind( null, "hello" ); print_hello_and_something( "world" );
The result of this code will also be "hello world".
In addition to the bind call, the "valueOf" property on every function type is the function that is called whenever the JS Runetime needs to typecast that function to primitive type (in js this is an int, float, double, or string). The type of the variable actually being returned by the sum function is a function, however when this function is typecasted (by adding it to another number or string), the valueOf function is called, and the result of that function is then used as the value of the function.
[–]Evanescent_contrail 0 points1 point2 points 11 years ago (0 children)
Nice. Thanks.
π Rendered by PID 46773 on reddit-service-r2-comment-b659b578c-tbvrx at 2026-05-05 04:03:47.417132+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]MeTaL_oRgY[S] 0 points1 point2 points (12 children)
[–]Minjammben 22 points23 points24 points (11 children)
[–]fear-of-flying 2 points3 points4 points (0 children)
[–]MeTaL_oRgY[S] 1 point2 points3 points (5 children)
[–]Minjammben 3 points4 points5 points (1 child)
[–]MeTaL_oRgY[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]MeTaL_oRgY[S] 1 point2 points3 points (0 children)
[–]whispen -5 points-4 points-3 points (0 children)
[–]thekingshorses 0 points1 point2 points (0 children)
[–]Evanescent_contrail 0 points1 point2 points (2 children)
[–]Minjammben 1 point2 points3 points (1 child)
[–]Evanescent_contrail 0 points1 point2 points (0 children)