Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function property vs method

Tags:

typescript

What practical differences are there between defining an interface method:

interface Foo {     bar(): void; } 

and defining a property with a function type:

interface Foo {     bar: () => void; } 

?

like image 537
Zev Spitz Avatar asked Aug 25 '16 23:08

Zev Spitz


People also ask

What is the difference between method and property?

In most cases, methods are actions and properties are qualities. Using a method causes something to happen to an object, while using a property returns information about the object or causes a quality about the object to change.

What is the difference between a method and a function?

A function is a set of instructions or procedures to perform a specific task, and a method is a set of instructions that are associated with an object.

What is the difference between methods and properties in JavaScript?

The difference between b/w property and method is that - property is a value stored in the hash key, whereas method is a function stored in the hash key.

What is the difference between method and property in Python?

As I understand it, property is calculated when an object is created. And method makes calculations when I call it.


1 Answers

If these are the only declarations, these are identical.

The only difference is that you can augment the first form in a second declaration to add new signatures:

// Somewhere interface Foo {   bar(): void; }  // Somewhere else interface Foo {   bar(s: number): void; }  // Elsewhere let x: Foo = ... x.bar(32); // OK 
like image 181
Ryan Cavanaugh Avatar answered Oct 15 '22 06:10

Ryan Cavanaugh