Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Class Methods | Classes Fundamentals
TypeScript Classes and OOP

bookClass Methods

1234567891011121314151617181920212223242526
class Calculator { add(a: number, b: number): number { return a + b; } subtract(a: number, b: number): number { return a - b; } multiply(a: number, b: number): number { return a * b; } divide(a: number, b: number): number { if (b === 0) { throw new Error("Cannot divide by zero."); } return a / b; } } const calc = new Calculator(); console.log(calc.add(5, 3)); // 8 console.log(calc.subtract(10, 4)); // 6 console.log(calc.multiply(2, 7)); // 14 console.log(calc.divide(20, 5)); // 4
copy

When you define a function inside a TypeScript class, that function is called a method. Methods are used to perform actions or calculations related to the class's purpose. You can declare methods by writing the method name, followed by parentheses and an optional parameter list, a colon, and the return type. The method body is enclosed in curly braces.

To call a method, create an instance of the class and use dot notation, such as instance.methodName(). Methods can return any type, including number, string, boolean, or even custom types. If a method does not explicitly return a value, its return type is void. Specifying the return type helps TypeScript check that your method returns the correct kind of value.

In the code above, the Calculator class defines four methods: add, subtract, multiply, and divide. Each method takes two numbers as parameters and returns a number. The divide method also checks for division by zero and throws an error if needed. When you create a new Calculator instance, you can call these methods to perform calculations.

question mark

What is a method in the context of a TypeScript class?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain how error handling works in the divide method?

What happens if I try to divide by zero using this Calculator class?

Can I add more methods to this Calculator class, like a power or square root function?

Awesome!

Completion rate improved to 5

bookClass Methods

Sveip for å vise menyen

1234567891011121314151617181920212223242526
class Calculator { add(a: number, b: number): number { return a + b; } subtract(a: number, b: number): number { return a - b; } multiply(a: number, b: number): number { return a * b; } divide(a: number, b: number): number { if (b === 0) { throw new Error("Cannot divide by zero."); } return a / b; } } const calc = new Calculator(); console.log(calc.add(5, 3)); // 8 console.log(calc.subtract(10, 4)); // 6 console.log(calc.multiply(2, 7)); // 14 console.log(calc.divide(20, 5)); // 4
copy

When you define a function inside a TypeScript class, that function is called a method. Methods are used to perform actions or calculations related to the class's purpose. You can declare methods by writing the method name, followed by parentheses and an optional parameter list, a colon, and the return type. The method body is enclosed in curly braces.

To call a method, create an instance of the class and use dot notation, such as instance.methodName(). Methods can return any type, including number, string, boolean, or even custom types. If a method does not explicitly return a value, its return type is void. Specifying the return type helps TypeScript check that your method returns the correct kind of value.

In the code above, the Calculator class defines four methods: add, subtract, multiply, and divide. Each method takes two numbers as parameters and returns a number. The divide method also checks for division by zero and throws an error if needed. When you create a new Calculator instance, you can call these methods to perform calculations.

question mark

What is a method in the context of a TypeScript class?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 3
some-alt