JS Value Objects – 2. Data types

Note: This is the second post in a series about the use of Value Objects in JavaScript.

In the previous post we learned what a Value Object (VO) is and what kind of problems it can solve. Now we can start exploring the possibilities in JavaScript (JS) for creating a similar pattern. In this post we will start with a brief view at data types.

Literals vs. Objects

JS provides 6 different basic types of values: numbers, strings, Booleans, objects, functions and undefined values (null and undefined).

Those primitives, with the exception of null and undefined, can be created from literals or from a constructor. The difference between those two methods of instantiating is not only the syntax, but also the actual value that it will produce. When using the loose equality operator you might not see this, but when you would compare the different methods with strict equality you will see that the value created from the constructor is an object and not a string (or number or Boolean).

This is a minor downside, but you should use the strict equality operator with care anyway. Another side-effect is that creating strings as objects is slower than creating it from literals. In general I would advice to use the literal way as much as possible.

Of course this does not count for objects, because an object is always only equal to itself. I will leave the  data type ‘function’ out of this discussion because they have a very special, multifunctional role in JS.

Note: Arrays are also objects and therefor not a primitive data type in JS. You can check this by using unary ‘typeof’ keyword.

Are Data Types Value Objects?

In the previous post we came across the 4 key properties of a VO: single concept, no life cycle, immutable and can not be referenced. Do these rules apply to the JS primitive data types?

Single concept

This rule most certainly applies. Just as much as in any other programming language. At least, for string, number, Boolean, object and function. Null is a bit of a strange value because it actually means that there is no value set and undefined is in fact nothing at all; it is, if one could say so, a not found value.

No lifecycle

For this rule it cannot be said just as easily, because it would mean that there could be only one ‘instance’ of every value that a data type could have. That is partly true:

According to this, we can conclude that strings created from literals do apply to the no life cycle rule, but strings created from objects do not. They have some kind of unique identifier that makes each instance different from another instance with exactly the same value. This is important to notice, because it conflicts with the description Eric Evans gave us in the first post; this is even better described by Martin Fowler:

Value Objects … their notion of equality isn’t based on identity, instead two value objects are equal if all their fields are equal. – Martin Fowler

We can test that this is not true for objects:

Note: Interesting is that we can compare objects if we convert both of them to a JSON string. This has a lot of limitations though; The objects can not contain methods (functions) and the order of the properties should be exactly the same.

Immutable

Immutability is also partly true for JS data types. As strings, numbers and Booleans cannot be mutated, objects can:

No refference

Another characteristic of objects is that they are always referenced and although it can give you a lot of headaches, this is actually one of the things that makes JS a special language. Referencing has a lot of benefits; it gives you a lot of freedom, but it also provides a door to hell where it causes nasty side-effects that are hard to trace back.

In the above example we see that John and Peter live in the same house, but when Peter moves and we try to change his address, we also change John’s address.

Summary

Taking this all into account, you could say that the literals for creating a string, number, boolean, null and undefined are real values, because they have the key properties. Object on the other hand is failing for at least 3 of the 4 rules, because you might also argue if object is even describing a single concept.

The funny part is that in the next post we will find out that we have to use objects to be able to create our own VOs. So stay tuned!

Further reading

Leave a Reply

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