A __proto__: Key Is Refused

Status: refused by design

__proto__: is not FunctionalScript. { __proto__: v } is valid JavaScript, and fjs compile refuses it by design: the demo below shows what it prints.

In an object literal, JavaScript reads __proto__: v and "__proto__": v not as a property but as the object's prototype, so { __proto__: [] } is an object that inherits from an array. FunctionalScript has no prototypes, and giving the key its own-property meaning would make accepted text mean something else than in JavaScript, so the two spellings are refused. The computed key, ["__proto__"]: v, is an own property in both languages, and is the spelling (objects).

The row acc.proto-key of refused constructs registers the refusal, and the __proto__ key gives its reason.

Examples

export default { __proto__: [] } instanceof Array;

The demo on this page compiles sample.mjs, the module above, with this build's fjs compile, and shows the line it prints and the token it refuses. Below that it shows what JavaScript answers for the same module: true.

What to Write Instead

For a property named __proto__, write the computed key, as alternative.f.js does:

export default { ["__proto__"]: [] } instanceof Array;

It answers false. The object owns a property named __proto__ and stays a plain object.

A Better Message

Today fjs compile prints __proto__ requires the computed key form at the refused token. The demo proposes what it should print instead: the reason at the same token, what JavaScript does there, and the allowed spelling, the line alternative.f.js writes, as help. The page compiles the alternative too, so the suggestion is one the compiler accepts.

Reference

Demo

Not allowed in FunctionalScript

This is valid JavaScript, and fjs compile refuses it, by design: it is not a FunctionalScript module.

What fjs compile sample.mjs output.js prints:

sample.mjs:1:18 - error: __proto__ requires the computed key form
1 | export default { __proto__: [] } instanceof Array;
  |                  ^

What it should print instead, pointing to what FunctionalScript allows (proposed):

sample.mjs:1:18 - error: a `__proto__:` key is not FunctionalScript: JavaScript makes it the prototype, not a property
1 | export default { __proto__: [] } instanceof Array;
  |                  ^^^^^^^^^ JavaScript makes `[]` the prototype here, so the object is an `Array`
  = help: for a property named `__proto__`, write the computed key:
          export default { ["__proto__"]: [] } instanceof Array;
  = note: refused by design, see spec/values/objects/proto-key/README.md

JavaScript source, sample.mjs:

export default { __proto__: [] } instanceof Array;

What JavaScript answers for the same module, its default export:

true

What to write instead

The FunctionalScript spelling, alternative.f.js:

/**
 * The FunctionalScript spelling of [`sample.mjs`](./sample.mjs): an own property named `__proto__`, spelled as a computed key
 * ([`README.md`](./README.md)).
 *
 * @module
 */

export default { ["__proto__"]: [] } instanceof Array;

What fjs compile alternative.f.js output.js writes:

export default {["__proto__"]:[]} instanceof Array;

What it answers, its default export:

false

Emergent Testing

Idle. Press Run to start the suite.

  • ./proof.f.mjs