guest-book-as icon indicating copy to clipboard operation
guest-book-as copied to clipboard

Revisit constructor in model that is likely unnecessary

Open mikedotexe opened this issue 4 years ago • 1 comments

At the time of this writing, here's the latest commit where we have: https://github.com/near-examples/guest-book/blob/0676d825b79be48ab7a00004ed055be09a42895d/assembly/model.ts#L10

  constructor(public text: string) {
    this.premium = context.attachedDeposit >= u128.from('10000000000000000000000');
    this.sender = context.sender;
  }

Not sure what the point of text is here, but I haven't been in the AS world for a while.

mikedotexe avatar Jun 25 '20 17:06 mikedotexe

I'm rather new to AssemblyScript too but it looks like constructors support shorthand instance property assignment. Essentially the public keyword means instances will have a property called "text" with a type of string. Without the public keyword, the parameter will be private so the constructor will have the flexibility to do whatever it wants with it.

In the TypeScript world (given it's more closely related syntactically than standard JavaScript), it's the equivalent to writing:

constructor(text: string) {
  this.text = text;
  this.premium = context.attachedDeposit >= u128.from('10000000000000000000000');
  this.sender = context.sender;
}

TLDR: Removing this parameter will mean messages won't have any contents other than the metadata (i.e. who sent it and whether a donation was attached).

lewis-sqa avatar Jan 12 '22 09:01 lewis-sqa