Disclaimer: random thoughts of mine; not sure if they are useful.
class StringBuilder {
#data = '';
proto add = logged @ function (str) {
this.#data += str;
};
proto toString = logged @ function () {
return this.#data;
};
}
function logged(func) {
return function (this, ...args) {
console.log(args);
return func.call(this, ...args);
};
}
-
Prototype fields:
proto key = value;
-
Function application operator:
f @ x === f(x)
- Alternative: postfix use of decorators via pipeline operator:
proto add = function (str) { this.#data += str; } |> logged;
- Alternative: postfix use of decorators via pipeline operator:
-
Optional
this
as an explicit parameter of universal functions:- Makes the use of
this
explicit in non-method functions.
- Makes the use of
Turns arrow functions into methods:
class StringBuilder {
#data = '';
proto add = logged @ (this, str) => {
this.#data += str;
};
proto toString = logged @ (this) => {
return this.#data;
};
}
function logged(func) {
return (this, ...args) => {
console.log(args);
return func.call(this, ...args);
};
}
- Can
logged()
access the names of the methods? Would be useful for logging!