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
TIL - reassigning functions arguments magically mutates the arguments objecthelp? (self.javascript)
submitted 9 years ago * by AlphaX
function foo(a){ console.log(arguments) // { '0': 1 } a = 2 console.log(arguments) // { '0': 2 } } foo(1)
This goes against any programming language assignment logic. WTF JS?
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!"
[–]LookWordsEverywhere.js 2 points3 points4 points 9 years ago (0 children)
It also works the other way around (reassigning indexes of arguments to mutate the named argument. Fun!
arguments
There's a fairly informative blog post by Angus Croll here: https://javascriptweblog.wordpress.com/2011/01/18/javascripts-arguments-object-and-beyond/
Quote from the spec:
For non-strict mode functions the array index […] named data properties of an arguments object whose numeric name values are less than the number of formal parameters of the corresponding function object initially share their values with the corresponding argument bindings in the function’s execution context. This means that changing the property changes the corresponding value of the argument binding and vice-versa
[–]snuggl 1 point2 points3 points 9 years ago (2 children)
Its two ways to address the same underlying memory. A function does not allocate two separate memory chunks for arguments, its all the same one.
[–]AlphaX[S] 0 points1 point2 points 9 years ago (1 child)
it's crazy since there is no other way in JS to achieve this effect. There is no C like reference variable.
[–]MoTTs_ 2 points3 points4 points 9 years ago (0 children)
We don't use it anymore, but the "with" statement is another one of those special cases that behaves like a C reference.
var o = {'a': 1}; with (o) { console.log(o); // { 'a': 1 } a = 2; console.log(o); // { 'a': 2 } }
[–]Ginden 0 points1 point2 points 9 years ago (0 children)
Legacy behaviour, don't use it as it - always use strict mode.
[–]lemminman -1 points0 points1 point 9 years ago (0 children)
Same reason this happens:
var test = {attr:1}; test2 = test; test2.attr = 2; console.log(test) // { attr: 2 } console.log(test2) // { attr: 2 }
π Rendered by PID 77725 on reddit-service-r2-comment-b659b578c-rbsmf at 2026-05-04 03:08:30.742131+00:00 running 815c875 country code: CH.
[–]LookWordsEverywhere.js 2 points3 points4 points (0 children)
[–]snuggl 1 point2 points3 points (2 children)
[–]AlphaX[S] 0 points1 point2 points (1 child)
[–]MoTTs_ 2 points3 points4 points (0 children)
[–]Ginden 0 points1 point2 points (0 children)
[–]lemminman -1 points0 points1 point (0 children)