Skip to content

Instantly share code, notes, and snippets.

@necccc
Created October 20, 2019 14:20
Show Gist options
  • Save necccc/84225415896af12d1be18d18db54d413 to your computer and use it in GitHub Desktop.
Save necccc/84225415896af12d1be18d18db54d413 to your computer and use it in GitHub Desktop.
Gist for When arguments are mutable
// 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
// 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"
var bar = function (a) {
var aa = arguments[0]
a = 'foo'
console.log(aa)
}
//this works as expected
bar(2) // 2
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