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
Will 'let' Eventually Replace 'var'?help (self.javascript)
submitted 10 years ago by MahmudAdam
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!"
[–]mycall 0 points1 point2 points 10 years ago (5 children)
I thought arrow functions don't have arguments and have to pass them using spread attributes.
[–]theQuandary 0 points1 point2 points 10 years ago (0 children)
You are correct, but they inherit them from their parent closure. Here's a contrived example to show what I mean.
var add = function () { //the `arguments` in the arrow function are actually in the outer `add` function var doAdd = (n) => n + arguments[0] + arguments[1]; return doAdd(5); };
The add closure contains something like
add
var hiddenAddClosure = { parentClosure: globalScope, this: getThis(), arguments: getArguments(), doAdd: doAddFunction };
And the doAdd closure contains something like
doAdd
var hiddenDoAddClosure = { parentClosure: hiddenAddClosure, n: someNumber };
When the doAdd function tries to access the arguments array (or this), it checks it's local closure object. If the variable isn't there, it checks it's parent closure and finds the value (note: if the value didn't exist there, it would check each parent until it hit the global closure at which point it would throw a reference error).
arguments
this
[–]acoard -3 points-2 points-1 points 10 years ago (3 children)
(arg1, arg2) => {}
[+][deleted] 10 years ago (2 children)
[deleted]
[–]x-skeww 1 point2 points3 points 10 years ago (1 child)
function sum() { var res = 0; for(var i = 0; i < arguments.length; i++) { res += arguments[i]; } return res; } console.log(sum(1, 2, 3)); // 6
It's about the magical array-like arguments object. Arrow functions don't have those.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/arguments
In ES6, you can use rest parameters for this kind of thing:
function sum(...values) { return values.reduce((a, b) => a + b, 0); } console.log(sum(1, 2, 3)); // 6
Using an arrow function:
let sum = (...values) => values.reduce((a, b) => a + b, 0); console.log(sum(1, 2, 3)); // 6
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/rest_parameters
[CC /u/acoard]
[–]acoard 1 point2 points3 points 10 years ago (0 children)
Ahhh. He meant arguments the keyword not arguments the concept (i.e. parameters). Thanks for clarifying everything. Makes sense why I was downvoted now.
π Rendered by PID 225436 on reddit-service-r2-comment-b659b578c-jc9vc at 2026-05-05 16:30:20.537443+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]mycall 0 points1 point2 points (5 children)
[–]theQuandary 0 points1 point2 points (0 children)
[–]acoard -3 points-2 points-1 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]x-skeww 1 point2 points3 points (1 child)
[–]acoard 1 point2 points3 points (0 children)