I some variables ing into my function.
If the first of these is a d3 selection I want to use it, otherwise I want to use a default selection.
How do I check if a variable is a d3 selection or not?
I some variables ing into my function.
If the first of these is a d3 selection I want to use it, otherwise I want to use a default selection.
How do I check if a variable is a d3 selection or not?
Share Improve this question asked Jan 4, 2013 at 22:24 George MauerGeorge Mauer 122k140 gold badges396 silver badges630 bronze badges 2- 1 Usually people use ducktyping for this. I.e. check if the variable has the properties you need and then assume it is the object you are looking for if it has certain properties. – ThiefMaster Commented Jan 4, 2013 at 22:26
-
Well the actual rebinding method is abstract since I don't want to repeat that code over and over (one of those functions that return a function dealies). I currently check for
d3SelectorOrNot.selectAll
but that seems rather gimpy. Most other libraries I've worked with provide anisMyLibraryObject()
function. I'm hoping d3 does too, I just can't find it. – George Mauer Commented Jan 4, 2013 at 22:36
2 Answers
Reset to default 10To check if variable sel
is a d3.selection:
var isselection = sel instanceof d3.selection;
Please note that the answer referenced above for (sel instanceof d3.selection)
, which is provided in the docs : https://github./mbostock/d3/wiki/Selections#d3_selection, will not work in IE9.
This is explained here : https://github./mbostock/d3/issues/851
It is an issue with IE9 and will not be fixed in D3. A workaround can be found in this mit : https://github./palantir/plottable/pull/637
using (typeof sel[0] !== "string")
instead. This isn't as clear but it will depend on if you need to support IE9 or not.