如何在控制器類Main
中實例化新類時構建JS代碼。使用參數創建類或分別調用其方法
解決方案:
答:傳遞參數,同時創造新的類 - new Options(args)
- 讓Options
的構造函數中調用它自己的方法。 B:創建新類並在對象上調用類的方法。
後來我在另一個類中使用Options
的屬性。
// A
class Main {
constructor(options) {
this.options = new Options(options);
{ firstProperty, secondProperty } = this.options;
this.another = new Another(firstProperty, secondProperty);
}
}
// B
class Main {
constructor(options) {
this.options = new Options();
const firstProperty = this.options.methodA(options);
const secondProperty = this.options.methodB(options);
this.another = new Another();
const anotherPropety = this.another.methodA(firstProperty);
(...)
}
}
這兩個代碼似乎並不相同。在B中,您不要將這兩個屬性複製到'this.another'。另外,如果'this.other'是'this.another'? – Barmar
它們是等價的,我應該添加'etc ...'。現在,它只是「另一個」,這是錯誤的,謝謝你發現它。 –