24 June 2019

#023 - Arrays are Objects, How Fascinating!

During my time learning the syntax of JavaScript, I learned the concept of objects and arrays, whilst grappling the Inception-esque of an array of objects.

Meanwhile, in C# it is evidently clear that arrays are objects due to the visually forced syntax, as noted by the 'new' keyword.

JavaScript:
let daysofWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

C#:
var daysofWeek = new string[6] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

It also answers my own with regards to a custom function that I wrote a while back when I was coding my JavaScript quiz. Initially, I had a function that created a new unordered array stack based upon the original array source. Within this function, I would push a new unordered item to the new array, whilst using the Array.prototype.shift() method to remove the first item of the original array source.

I retained this approach, as I thought it would improve the memory performance of the application. However, I learned that JavaScript and C# both have a 'Garbage Collection' feature, which means you don't need to deal with memory management - as objects that are not used in your code are simply discarded and not stored in memory.

If I was coding the side project using Objective-C or C++, then I would need to implement memory management. It may explain why certain video games can crash due to memory leaks, because AAA video games are typically programming in C++.

~Richard