0
0
Typescriptprogramming~10 mins

Declaring functions and classes in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Declaring functions and classes
Start
Declare Function
Call Function?
NoEnd
Yes
Execute Function Body
Return Value
Declare Class
Create Object
Use Object Methods/Properties
End
This flow shows declaring a function, optionally calling it, then declaring a class, creating an object, and using its methods or properties.
Execution Sample
Typescript
function greet(name: string): string {
  return `Hello, ${name}!`;
}

class Person {
  constructor(public name: string) {}
  greet() { return `Hi, I am ${this.name}`; }
}
Declares a function greet that returns a greeting string, and a class Person with a name property and a greet method.
Execution Table
StepActionEvaluationResult
1Declare function greetFunction greet(name) createdFunction stored in memory
2Declare class PersonClass Person created with constructor and greet methodClass stored in memory
3Call greet('Alice')Execute greet with name='Alice'Returns 'Hello, Alice!'
4Create new Person('Bob')Call constructor with name='Bob'Object with name='Bob' created
5Call person.greet()Execute greet method on Person objectReturns 'Hi, I am Bob'
6EndNo more actionsProgram ends
💡 All declarations done and function/class usage demonstrated, program ends.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
greetundefinedfunction greetfunction greetfunction greetfunction greet
Personundefinedclass Personclass Personclass Personclass Person
personundefinedundefinedPerson object {name:'Bob'}Person object {name:'Bob'}Person object {name:'Bob'}
greet('Alice') resultundefined'Hello, Alice!''Hello, Alice!''Hello, Alice!''Hello, Alice!'
person.greet() resultundefinedundefinedundefined'Hi, I am Bob''Hi, I am Bob'
Key Moments - 3 Insights
Why do we declare the function before calling it?
Because in the execution_table at step 1, the function greet is created and stored in memory, so it can be called later at step 3. If we call it before declaration, it would cause an error.
What happens when we create a new Person object?
At step 4, the constructor runs with the given name, creating an object with that name property. This is shown in the variable_tracker where person changes from undefined to an object.
How does the greet method access the name property?
In step 5, person.greet() uses 'this.name' to access the object's name property, returning a string with the name. This shows how methods use 'this' to refer to the current object.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of calling greet('Alice') at step 3?
A'Hi, I am Alice'
Bundefined
C'Hello, Alice!'
DError
💡 Hint
Check the 'Result' column at step 3 in the execution_table.
At which step is the Person object created?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Action' column for object creation in the execution_table.
If we change the name in new Person('Bob') to 'Eve', what changes in variable_tracker after step 4?
Aperson becomes undefined
Bperson becomes {name: 'Eve'}
Cgreet function changes
DNo change
💡 Hint
Check the 'person' variable values in variable_tracker after step 4.
Concept Snapshot
Declaring functions:
function name(params): returnType { body }

Declaring classes:
class Name { constructor(...) {...} methods... }

Functions run when called.
Classes create objects with properties and methods.
Use 'this' inside class methods to access object data.
Full Transcript
This visual execution shows how to declare functions and classes in TypeScript. First, the function greet is declared and stored in memory. Then the class Person is declared with a constructor and a greet method. When greet('Alice') is called, it returns a greeting string. Creating a new Person object runs the constructor to set the name property. Calling person.greet() returns a greeting using the object's name. Variables change as functions and objects are created and used. This step-by-step trace helps understand how declarations and calls work in TypeScript.