最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论