के साथ तीर फ़ंक्शंस का उपयोग करना क्या यह संभव है? मुझे यकीन नहीं है, क्योंकि डी 3 this
rebindings का भारी उपयोग करता है और यह ES6 spec के साथ संघर्ष करता है।डी 3
उदाहरण के लिए निम्नलिखित ठीक काम करता है:
// 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