⊗tsSpOpSMt 16 of 37 menu

Static Methods in TypeScript

Not only properties, but also methods can be static. Such methods can be called without creating an object of the class.

Let's look at an example. Let's make a class that sums up the elements of an array. Let's make a static method in it that takes an array as a parameter and returns the sum of the array elements:

class Calc { public static getSum(arr: number[]): number { let sum: number = 0; for (let elem of arr) { sum += elem; } return sum; } }

Let's use this method to find the sum of the elements of some array:

let num: number = Calc.getSum([1, 2, 3, 4, 5]); console.log(num);

Add methods to the Calc class that will find the sum of squares and the sum of cubes of array elements.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline