liveBook · Manning

Arguments aliasing

As explained in Secrets of the JavaScript Ninja, Second Edition

The non-strict-mode behavior in which indexed values of the arguments object and corresponding function parameters share changes.

In non-strict JavaScript, an indexed value in the arguments object and its matching function parameter reflect changes to each other. Strict mode disables this aliasing. section 4.1.1

Listing 4.3. The arguments object aliases function parameters
function infiltrate(person) {
  assert(person === 'gardener',             #A
    'The person is a gardener');            #A
  assert(arguments[0] === 'gardener',       #A
    'The first argument is a gardener');    #A

  arguments[0] = 'ninja';                   #B
                                            #B
  assert(person === 'ninja',                #B
    'The person is a ninja now');           #B
  assert(arguments[0] === 'ninja',          #B
    'The first argument is a ninja');       #B

  person = 'gardener';                            #C
                                                  #C
  assert(person === 'gardener',                   #C
    'The person is a gardener once more');        #C
  assert(arguments[0] === 'gardener',             #C
    'The first argument is a gardener again');    #C
}

infiltrate("gardener"); 

The infiltrate example changes arguments[0], then the parameter, and checks that each change appears in both places. section 4.1.1

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Traces changes between an indexed argument and its matching parameter in both directions, then contrasts strict-mode behavior.
Examples
Uses an infiltrate function to change gardener to ninja through arguments[0], then changes the parameter back.
What you can do after
Understand how changes to an indexed argument affect its matching parameter, and how strict mode prevents that link.
How it connects
Connects the arguments object with function parameters and strict mode.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.