|Written by Ian Elliot|
|Thursday, 07 May 2015|
Page 1 of 4
There is a newer version of the draft of the book here.
Type is all about characterizing the properties an object has.
This works in different ways at compile time and at runtime.
The subtle point of compile time type checking is that it can provide an assurance that everything is ok at run time. If you have a variable of a given type and you don't cast it to another type then you can be fairly sure that if it passes compile time checks then it will be ok at runtime.
In some situations this is by far the best thing to do but occasionally the need to test many properties becomes unwieldy and we need a better way. Once again it is time to go back to the role of prototype inheritance which can be used to test for the possession of large sets of properties very efficiently. To make it work elegantly we need to take a slightly different approach to object construction - the prototype constructor.
Why So Much Runtime Type Testing?
In a class based language much of the type checking occurs at compile time and this provides a measure of protection at runtime.
For example, if you declare a variable to be of one type and you try and make it reference an incompatible type you will generate a compiler error:
Assuming that SomeOtherClass isn't a subclass of MyClass then this program will never get to run because the compiler will throw up a type error.
In the same way in a class based language any attempt to use a property that isn't defined within the declared type is flagged up at compile time. For example,
will never get to run because the compiler will complain that a String doesn't have a random method (assuming it doesn't of course).
So the first typical error i.e. assigning the wrong type to a variable can't happen at any time.
You will see a runtime error telling you that random is an undefined property and not a function.
However as long as they are detected while you are testing the program they are just as easy to understand and just as easy to fix. The reason is that they are static errors - mystring is a String and you can deduce this just by reading the code. As you know a String doesn't have a random method you can see the code is incorrect. You don't need to run the program to know that there is an error.
Of course there are some errors are not detectable at compile time because the type of the object being referenced isn't determined until runtime i.e. the type is dynamic and in this case the only possibility is runtime detection.
Forget Type - Use Duck Testing
If it walks like a duck and quacks like a duck then it is a duck.
You could say more accurately that it is enough of a duck for our purposes.
If you want to check if an object has a set of properties then why not just test to see if it has - i.e. test to see if if quacks.
This is very easy, works without complication and works at runtime.
For example suppose you are passed an object and you want to use prop. If prop hasn't been defined on the object or its prototype chain then
is undefined so you can test.
This is easy and unless you are testing lot of properties repeatedly it is efficient. If you are checking lots of properties repeatedly just do it once and set a validated property so that you don't do it again.
The is one additional problem the property might exist i.e. might be defined but it might have the wrong type of value.
This usually occurs when you do something like this:
only to discover a runtime exception because method isn't a function object and so cannot be called.
This is where primitive type enters the picture.
Testing Primitive Type - typeof
The typeof operator will return a string that gives the primitive type of an object.
Mozilla lists the following results for typeof:
This seems reasonable apart from perhaps Null being an object. Unfortunately not all browsers implement the strict definition of a function object as something you can call. In particular Chrome to version 12 defined a regular expression as a function.
Using typeof you can test to see not only that a property exists but that it is of the correct type. For example:
This tests that obj.method exists and that it is a function. If it doesn't exist typeof returns the string "undefined".
An alternative and slightly more secure method for a Function object is to test first to see if the property exists and then test to see if the constructor is what you expect. For example:
This first tests that method exists and then that it is a function by checking the constructor.
You can use this method for general objects where you set a custom constructor as long as you always remember to set the constructor property.
|Last Updated ( Tuesday, 25 August 2015 )|