JavaScript Objects

Introduction

An object in JavaScript can be seen on an technical level as an associative array  which can hold any type including itself. Through this property objects in JavaScript could be called Variants or tagged Unions except JavaScript objects can an hold more than one variable with their correct value at a time which true Variants or tagged Unions known from C/C++ cannot.

As said before an object can hold any type which is provided by the language. Those types can be categorized into two groups: the primitive and the complex.

Primitive Types:

  • numbers
  • booleans
  • strings
  • undefined

Complex Types:

  • object
  • array
  • function

To find out which types you are dealing with at runtime JavaScript gives you the typeof operator. This operator returns a string containing the name of the type you test. It is possible to serialize an object. Serialized objects are becoming strings which contain all necessary information in the so called JavaScript Object Notation or short known as JSON. In other terms an JSON-Object is an stringyfied version of an normal object which is stored in memory.

Excursion: JSON

JSON is a widely used file format to exchange Information across different platforms. The format is quite simple. For example an empty object is simply represented as two curly brackets with no content between them.

Each Value stored in an object has an key associated with an Value, hence the object seen as associated array. Key and Value are divided by ‘:’ and each Key Value pair is separated with ‘ , ‘.

Back to our original Topic

It is possible to add new Key/Values pairs to an object at runtime any time you wish.  In other words you are able to attach any kind of variable to an object. Therefor you have different syntactical ways to express this. Values of an Object are also called properties or items!

Way Number One via the dot operator

Way Number Two via the Index operator

Both ways are equivalent. If the pair is not present it will be created and add to the object. To access any item of an object you simple use the very same syntax you used to create them. But do not conclude the way to access an item is bound by the way you added the item, this is NOT the case. It does not matter which way you attached an item to an object to later access it.

There is only one exception to this! If you use an integer as index you have created an special object, the so called array object. The array is one of the built-in types  of JavaScript and is discussed in an later post.

Another way to access items from an object is to traverse through those items. JavaScript offers three ways to do it:

  • for...in loop
  • Object.keys( obj )
  • Object.getOwnPropertyNames( obj )

Sometimes you want the very opposite. You do not wish to add but remove one item from the object. You can remove it with help of the delete operator.

But be aware this is a ridiculous slow operation. The use of this operator looks like this:

because of the performance loss if used their is an alternative. This alternative is to set the value of the item to undefined.   It does not really remove the item because the key stays in the object and is still accessible but the value will be deallocated at some point by the GC. You save a lot memory without the costly call of the operator delete

More about deleting in another post.

 

 

 

 

 

2 Comments

  1. Pingback: JSON -> JavaScript Object Notation – Johannes Lüke

  2. Pingback: JavaScript -> Object Model – Johannes Lüke

Leave a Reply

Your email address will not be published. Required fields are marked *