![](/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.