Created
October 20, 2019 14:20
-
-
Save necccc/84225415896af12d1be18d18db54d413 to your computer and use it in GitHub Desktop.
Gist for When arguments are mutable
This file contains hidden or 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
// using strict mode | |
var fn3 = function (a) { | |
"use strict"; | |
a = "foo" | |
console.log(arguments[0]) | |
} | |
// arguments, and the arguments object no longer track each other | |
fn3(2) // 2 | |
// containing rest, defaults or destructured parameters | |
var fn4 = function (a = 0) { | |
arguments[0] = "bar" | |
console.log(a) | |
} | |
fn4(2) // 2 | |
var fn5 = function (...args) { | |
arguments[0] = "bar" | |
console.log(args[0]) | |
} | |
fn5(2) // 2 | |
var fn6 = function () { | |
const [a] = arguments | |
arguments[0] = "bar" | |
console.log(a) | |
} | |
fn6(2) // 2 |
This file contains hidden or 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
// non-strict function, not containing rest, | |
// defaults or destructured parameters | |
var fn1 = function (a) { | |
a = "foo" | |
console.log(arguments[0]) | |
} | |
// argument `a` tracks arguments[0] | |
fn1(2) // "foo" | |
var fn2 = function (a) { | |
arguments[0] = "bar" | |
console.log(a) | |
} | |
// arguments[0] tracks argument `a` | |
fn2(2) // "bar" |
This file contains hidden or 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 bar = function (a) { | |
var aa = arguments[0] | |
a = 'foo' | |
console.log(aa) | |
} | |
//this works as expected | |
bar(2) // 2 |
This file contains hidden or 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 bar = function (a) { | |
a = 'foo' | |
console.log(arguments[0]) | |
} | |
bar(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment