javascript - Is there to check if a js variable is a d3 selection? - Stack Overflow

I some variables ing into my function.If the first of these is a d3 selection I want to use it, otherwi

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 an isMyLibraryObject() function. I'm hoping d3 does too, I just can't find it. – George Mauer Commented Jan 4, 2013 at 22:36
Add a ment  | 

2 Answers 2

Reset to default 10

To 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744324585a4568582.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信