-
Notifications
You must be signed in to change notification settings - Fork 208
Arrow Function Style
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,
});
Look for name =
or name:
.
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 theirthis
. If applied to anything other than their instance'scontext
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