I'm writing some charting components using D3. Some of these components are deliberately generic, so they can be re-used in various scenarios.
In one function, I receive a scale object as an argument. I want to take slightly different actions depending on whether this is a linear scale, a time scale, or an ordinal scale.
However I cannot easily see how to detect which type of scale I've got.
instanceof
does not work - these objects aren't create withnew
and don't have a prototype set. They are Function objects with additional attributes added to them.typeof
returns 'object' in all cases (naturally)at least in the case of ordinal scales, there are some additional attributes that I can use for detection, so I can use:
if (scale.rangeBound) { // do something specific to ordinal scales }
but that does not seem to be the case for different types of quantative scales, which seem to have identical sets of attributes.
I can add some attribute to each scale I create indicating it's type, but I would prefer not to as it reduces the generality of the functions I am creating (they would require scales passed to them which had these attributes added). Also I would risk clashing with some attribute added by a future version of D3.
In fact this question could be extended to many of the objects within D3. Is there any way to tell them apart?
You could insulate yourself against code changes by coding your check against the behaviour of the scales, than it's internals
Note, that this would still trip up for other / custom scales that have the same behaviour.
Fiddle - http://jsfiddle.net/enon2rjs/