Learning JavaScript part 3 Objects part 1

Note: As you can see this post is part 1 of a sub-series about objects and object model in JavaScript.

Even though I used object-oriented programming (OOP) paradigm countless times in the past JavaScript’s object model is at the moment a bit unclear. But before we dive into creating our own objects let’s learn the basics with predefined ones.

Note: Some parts will be explained later because they might be confusing at a time.

Arrays

In general arrays are indexed and/or named sets of values usually of one data type. I use this definition because that is what I am used to from writing code in C-like languages. But JavaScript unlike Java is weak typed language and doesn’t have an explicit arrays data type.

Also unlike Java in JavaScript an array is a predefined object which can be created in three different ways:

First two cases use directly the Array object and third uses simple enumeration of elements inside square brackets.

As in other languages it is possible to create uninitialized, elements have default values, array of specified length:

JavaScript uses numbers for indexing arrays and first element has index 0 (zero).

In Example 4 we create a new array and begin to populate it with elements starting at position 0.  length  is a property of object Array. In next example we’ll see similar code:

Example 5 produces different result. It is because  length ‘s value is implicitly defined as highest index plus one. But as we’ve seen in Example 2 it can be set to specific value.

Another thing I’d like to mention about populating arrays is that one can assign elements of various data types to different positions in the array:

I advise against it.[ref]Remember that you’ll have to write more code which detects types of values and comments explaining structure of said array. Respect that coder should write readable code for other not just for themself.[/ref] When I need to store different data types together I rather use JSON or objects defined by myself.

Array methods

JavaScript provides several handy methods I didn’t know about and a lot I used before. Here is a list of those I have found interesting:

  • concat()
    • turns two arrays into one

 

  • join(deliminator = “,”)
    • joins all elements into a string separated by deliminator

 

  • push() and pop()
    • work as when one works with stack
  • sort()
    • sorts array with default algorithm or user defined algorithm pass as a callback function

 

  • every(callback[ref]callback is a function which you can call when ever you execute some other function or method.[/ref]) and some(callback)
    • I haven’t used these functions yet but they’ll come handy one day. They return true if all/at least one gives true value from call back.

At this point I would like to go to loops for a bit. There are two forms of for  loop

for…in

Iterates a variable over all properties in an object

This loop is not recommended for iteration over the Array object because we assign variable a name of the property and then we have to access it again inside the loop.

for each…in

This loop is better suited for iteration over arrays because it iterates over property value and not names.

I am mentioning these loops here because Array object has method  forEach(callback)  which is a part of JavaScript’s functional programming traits. And I didn’t want it far away from  for each...in .

Conclusion

I left out multidimensional arrays simply because it is an array of arrays. That’s the only reason. In next part we’ll dig into  Function  object.