Comp 110 Objects

When a class is declared...

...add the name of the class the the appropriate frame on the stack, and draw an arrow to a "class" box on the Heap.

When a function is declared...

...add the name of the function the the appropriate frame on the stack (usually Globals), draw an arrow to a "fn" box on the Heap.

Reference Local Reference Variables for an example on calling functions using environment diagrams.  These concepts, as well as Local Primitive Types, Global Variables, Primitive Parameters, and Reference Parameters, will all be referenced in the below example.

When a new object is constructed...

...establish the value of the object on the heap using a similar syntax as you would an array. If the object has default property values, assign these on the heap (even if they are primitives!)

Add the name of the new object to the appropriate frame, and draw a pointer to the corresponding object on the heap.

Example:

class TwitterProfile {
    followers: number = 0;
    tweets: number = 0;
}

let main = async () => {
    let krackleProfile = new TwitterProfile(); // break 1
    viralTweet(krackleProfile); // break 2
// break 4
};

let viralTweet = (tp: TwitterProfile): void => {
     tp.tweets++;
     tp.followers += 10; // break 3
};

main();

// AT BREAK 1:

In the above diagram, the main function has been called, krackleProfile has been declared and initialized to a new TwitterProfile object.

// AT BREAK 2:

viralTweet(krackleProfile) has been called, and the local reference parameter tp points to the same object as krackleProfile in main().

// AT BREAK 3

the tp local reference's followers and tweets properties (that are primitives) are accessed and altered. When viralTweet is finished executing...

print(krackleProfile.followers); 
print(krackleProfile.tweets);  

...at break 4 will print 10 and 1 respectively