[go: nahoru, domu]

Skip to content

Latest commit

 

History

History
174 lines (120 loc) · 4.63 KB

File metadata and controls

174 lines (120 loc) · 4.63 KB

ECMAScript Const Function Arguments

This proposal introduces constant function argument references, which allows for better enforcement of explicit variable mutability.

Status

Stage: 0
Champion: tbd

For more information see the TC39 proposal process.

Authors

  • Jeremiah Senkpiel (@fishrock123)

Proposal

When declaring a function or an arrow function, adding const before an argument declaration makes the argument reference immutable, in the same manner as if the argument was a variable declared by the const variable declaration keyword. Just like const variables, the immutability is only by reference for objects, and does not extend deeply.

function (const a) {
  const b

  // a is immutable in the same way b is
}
(const a) => {
  const b

  // a is immutable in the same way b is
}

Interaction with default arguments

Default arguments still function as normal. The immutability happens once the function body begins, and default argument statements are treated as the declaration assignment.

function (const a = 'hello') {
  const b

  // a is immutable in the same way b is
  // if `a` was not passed to the function, `a` will be 'hello'
}

Additionally, the following example:

function outer (
    const a = 'hello',
    inner = (function inner (a = 'world') {
      console.log('inner', a)
    })()
  ) {
  console.log('outer', a)
}
outer()

Acts in the same way as:

function outer () {
  const a = 'hello'
  function inner (a) {
    a = 'world'
    console.log('inner', a)
  }
  inner()
  console.log('outer', a)
}
outer()

Interaction with the arguments object

Constant function argument make the direct argument references immutable. This does not impact the arguments object, in strict or sloppy mode, which hold its own references and may be mutated as normal.

In sloppy mode, assigning to the arguments object will not update a constant function argument.

fn(1)

function fn (const a) {
  // a is 1

  arguments[0] = 2
  // a is 1
  // arguments[0] is 2
}

"What about let and var?"

This proposal only touches what is currently missing. Neither let or var are necessary as that is already the default behavior.

The proposal has no impact on lexical or function scoping. As arguments are always declared before the function body, the lexical scope is always the function body.

Open Questions

Would it be possible to use const function arguments in an arrow function that has just one argument and no parentheses?

const myFunc = const a => {
  const b

  // a is immutable in the same way b is
}

Potential short-hand options

Is a short-hand version desirable? What could be used that is still clear on its purpose?

Grammar

TODO

Resources

TODO?

TODO

The following is a high-level list of tasks to progress through each stage of the TC39 proposal process:

Stage 1 Entrance Criteria

  • Identified a "champion" who will advance the addition.
  • Prose outlining the problem or need and the general shape of a solution.
  • Illustrative examples of usage.
  • High-level API (proposal does not introduce an API).

Stage 2 Entrance Criteria

Stage 3 Entrance Criteria

Stage 4 Entrance Criteria

  • Test262 acceptance tests have been written for mainline usage scenarios and merged.
  • Two compatible implementations which pass the acceptance tests: [1], [2].
  • A pull request has been sent to tc39/ecma262 with the integrated spec text.
  • The ECMAScript editor has signed off on the pull request.

Misc

Proposal formatting taken from rbuckton's proposal-shorthand-improvements.