this Is Refused

Status: refused by design

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

JavaScript's this is an input a function receives from how it is called rather than from its arguments: o.f() passes o, (0, o.f)() passes undefined, and an arrow takes the this of the code around it, which at a module's top level is undefined. FunctionalScript keeps a function's inputs to its parameters, so the value a function reads is the value it was given, and this is refused by design.

The row op.this of refused constructs registers the refusal, and design principles gives its reason.

Examples

export default typeof this;

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: undefined in Node.js, Deno and browsers, where a module's this is undefined, and object in Bun, which gives it one: even the engines do not agree on what this is.

What to Write Instead

Pass the value as a parameter, as alternative.f.js does:

/** @type {(o: { readonly n: number }) => number} */
const get = o => o.n;
export default get({ n: 1 });

It answers 1. The object is an argument, so what the function reads does not depend on how it is called.

A Better Message

Today fjs compile prints reserved word 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:23 - error: reserved word
1 | export default typeof this;
  |                       ^

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

sample.mjs:1:23 - error: `this` is not FunctionalScript: a function's inputs are its parameters
1 | export default typeof this;
  |                       ^^^^ JavaScript answers by how the code is called; at module level, `undefined`
  = help: pass the value as a parameter:
          const get = o => o.n;
  = note: refused by design, see spec/operators/this/README.md

JavaScript source, sample.mjs:

export default typeof this;

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

undefined

What to write instead

The FunctionalScript spelling, alternative.f.js:

/**
 * The FunctionalScript spelling of [`sample.mjs`](./sample.mjs): the object a method would read through `this`, passed as a parameter
 * ([`README.md`](./README.md)).
 *
 * @module
 */

/** @type {(o: { readonly n: number }) => number} */
const get = o => o.n;
export default get({ n: 1 });

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

const $0=($1)=>$1.n;export default $0({"n":1});

What it answers, its default export:

1

Emergent Testing

Idle. Press Run to start the suite.

  • ./proof.f.mjs