Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Class Methods | Classes Fundamentals
Quizzes & Challenges
Quizzes
Challenges
/
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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 3

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

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

Свайпніть щоб показати меню

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 3
some-alt