![](/img/trans.png)
[英]Why the output is a string array even after assigning the variable as number in typescript?
[英]Why to declare variable in the class, even assigning that variable to this in the constructor
在下面的代码中,正如您所看到的,我在Test
类中声明了一个变量constObj
。 同时在构造函数中this
指定constObj
对象。
为什么我们需要再次声明的变量,即使我们已经将其分配给this
构造函数?
在webstorm
IDE中,抛出错误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();
如果我们不申报,请告知,将会发生什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.