-1

i'm trying to use class on Office Scripts (Excel Online). constructor() produces "Unexpected strict mode reserved word". could someone help me?

function main(workbook: ExcelScript.Workbook)
{
  class Foo{
    constructor(){
      console.log("Foo!");
    }
  }
  const foo = new Foo();
}

code editor screenshot "Unexpected strict mode reserved word"

4

1 回答 1

0

构造函数中似乎不允许该操作。您可以在对象的构造函数中提供一个字符串,将其分配给类中的私有字段,然后使用方法打印该字符串。所以像:

  function main(workbook: ExcelScript.Workbook) {
class Foo {
  private foo : string
  constructor(foo) {
    this.foo = foo;
  }
  bar() {
    console.log(this.foo)
  }
}
const foo = new Foo("Foo!");
foo.bar() //Foo!

}

于 2021-08-05T02:29:33.797 回答