Skip to content

Arrow Function Style

Mark S. Miller edited this page Sep 3, 2022 · 7 revisions

JavaScript has several syntaxes for functions / methods. For consistency, conciseness, and security (#524), we use arrow function syntax in our preferred dialect, Jessie:

const foo = (a, b) => a + b;
harden(foo);

const bar = async (a, b) => a + b;
harden(bar);

export const baz = (a, b) => a + b;
harden(bar);
const obj = harden({
  add: (a, b) => a + b,
});

Finding without function foo

Look for name = or name: .

Far Classes do not use Arrow Function Style

With the coming of Far Classes https://github.com/Agoric/agoric-sdk/pull/5960 (which may instead be named "Exo Classes" https://github.com/Agoric/agoric-sdk/pull/6118 ), we write the methods in concise method syntax, where these methods get their instance-specific information (their context object) as their this binding. These methods must only be written inline within the Far class definer (like vivifyFarClass) and must never be passed around as first class values. This is because

  • by themselves, they are not defensive. They depend on their interface guard as their first line of defense.
  • they are this sensitive. Without their protective outer shell, they do not validate their this. If applied to anything other than their instance's context they can misbehave in surprising ways.

See https://github.com/Agoric/agoric-sdk/discussions/6113

So, even though this is an exception to our general only-arrow-function rule, it is still the case that all first class functions should be written only as arrow functions.


category: Coding Style

Clone this wiki locally