Giter Club home page Giter Club logo

proposal-const-function-arguments's Introduction

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.

proposal-const-function-arguments's People

Contributors

fishrock123 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

rudxain

proposal-const-function-arguments's Issues

What about destructuring?

For example:

function f({ foo, bar: {xyz} }) {
  console.log(foo, xyz);
}

I suppose it should be like this:

function f(const { foo, bar: {xyz} }) {
  console.log(foo, xyz); // both are const
}

`let`

By using let, attempting to redeclare via var would throw an error. Currently, this is not the case, because fn params behave as if they're declared with var (by default), so I propose supporting let too

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.