How to get layer type in OpenLayers 3?

If anyone gets stuck with identifying a layer's type in OpenLayers 3, there is a more sophisticated JavaScript method to achieve this. As layer objects are created with layer constructors, the native instanceof JavaScript function can be used to check for layer type.

As the identification of a layer type usually gets into an if or switch clause, one could easily check for the presence of the constructor's prototype object in the layer, instead of storing the type as a property on construction.

var groupLyr = new ol.layer.Group();
groupLyr instanceof ol.layer.Group;
//true
groupLyr instanceof ol.layer.Vector;
//false

I don't even think being able to extract the T and V, if possible, is gonna be reliable cross-browsers. Might just be a browser interpretation thing. In any case, if you have access to it, I would just add your own attribute when you're creating the layer:

var foo_layer = new ol.layer.Vector(...);
foo_layer.layer_type = 'vector';

Then you can use it later in your code:

map.getLayers().a[0].layer_type
// 'vector'

Would that work for you?