可能ですか? d3はthis
の再バインドを頻繁に使用するため、これはES6 specと競合しているようです。例えばd3で矢印関数を使用
は、以下では正常に動作します:
// Working fine
var data = [1,2,3]
var svg = d3.select('body').append('svg').attr('height', 500).attr('width', 500).style('background-color', 'orange');
var gs = svg.selectAll('g').data(data).enter();
gs.append('circle')
.attr('cx', function() { return Math.random()*500; })
.attr('cy', function() { return Math.random()*500; })
.attr('r', function() { return Math.random()*100; })
.each(function() { console.log(this); }); // this is bound to the current element in the enter selection
(this
が入力選択で現在の要素にバインドされたが、Window
対象にされていない)期待どおりに動作しません、次の中:
var data = [1,2,3]
var svg = d3.select('body').append('svg').attr('height', 500).attr('width', 500).style('background-color', 'blue');
var gs = svg.selectAll('g').data(data).enter();
gs.append('circle')
.attr('cx',() => Math.random()*500)
.attr('cy',() => Math.random()*500)
.attr('r',() => Math.random()*100)
.each(() => console.log(this)); // this is bound to Window object
関連フィドルhere。
@chriskellyあなたの例では、単純に(それは私が達成したいものではありません、選択範囲内のすべての要素に対して同じ値を設定し、そのため、関数は要素単位で使用されます) – jarandaf
私があなたのサンプルを実行すると、表示される唯一の違いは出力コンソールログです。しかし、なぜあなたは ''この ''にアクセスしたいですか?これは興味深い質問ですが、問題となるケースを知りたいと思います。 – chriskelly
@chriskellyそれはかなり具体的な使用例です(svgグループ内の要素の複雑な構造を作成するために再利用したい機能をいくつか持っていますが、私が参照しているグループ、新しい要素が追加される)。 – jarandaf