Jav*_*SKT 3 javascript typescript
在下面的代码中,您可以看到我constObj在Test类中声明了一个变量.并同时在构造函数中分配constObj对象this.
为什么我们需要再次声明变量,即使我们已经this在构造函数中将其赋值?
在webstormIDE,它抛出的错误doesn't have the property constObj,如果我这样做this.constObj,如果变量没有声明.但代码工作正常没有问题.
声明变量是强制性的,即使我们将其赋值给 this
const constObj = {
a: function() {
console.log("sivakumar");
}
};
class Test {
constObj: any; // Is this line mandatory? I mean declaring it???
constructor() {
Object.assign(this, {
constObj
});
}
callMethod() {
this.constObj.a();
}
}
new Test().callMethod();Run Code Online (Sandbox Code Playgroud)
如果我们不申报,请告知,将会发生什么.
| 归档时间: |
|
| 查看次数: |
43 次 |
| 最近记录: |