D3.js d3.选择类型检入IE

D3.js d3.选择类型检入IE,d3.js,D3.js,如何检查给定对象是否为d3选择 以下代码在Chrome和Firefox中打印true,但在Internet Explorer中打印false: console.log(d3.select(document.body) instanceof d3.selection) 更新2017-01-17 随着D3v4的发布,这个问题已经消失了(): 选择不再使用子类数组;它们现在是普通对象,提高了性能 API明确指出: d3.选择() […]此函数还可用于测试选择(d3.selection的实例) 使用新

如何检查给定对象是否为
d3
选择

以下代码在Chrome和Firefox中打印
true
,但在Internet Explorer中打印
false

console.log(d3.select(document.body) instanceof d3.selection)
更新2017-01-17 随着D3v4的发布,这个问题已经消失了():

选择不再使用子类数组;它们现在是普通对象,提高了性能

API明确指出:

d3.选择()

[…]此函数还可用于测试选择(
d3.selection的实例)

使用新版本,以下代码将在所有浏览器中实际计算为true:

d3.select() instanceof d3.selection   // true in Chrome, FF, IE
对于所有仍然在v3上的人,下面的原始答案有一个分析和解决问题的方法


问题 由于D3的内部工作原理,支持的每个浏览器都将打印
true
,而不支持
的浏览器将打印
false
。检查一下,很明显
d3.select = function(node) {
  // ... removed for brevity

  return d3_selection([group]);
};
function d3_selection(groups) {
  d3_subclass(groups, d3_selectionPrototype);
  return groups;
}
var d3_subclass = {}.__proto__?

// Until ECMAScript supports array subclassing, prototype injection works well.
function(object, prototype) {
  object.__proto__ = prototype;
}:

// And if your browser doesn't support __proto__, we'll use direct extension.
function(object, prototype) {
  for (var property in prototype) object[property] = prototype[property];
};
// Include this at the start of your script to include the
// property in any selection created afterwards.
d3.selection.prototype.isD3Selection = true;

console.log(d3.select(document.body).isD3Selection);   // true in any browser