2013-03-21 73 views
2

努力学习D3.js和JavaScript等东西:) 因此,这里是我在书中看到的代码:我用如何通过功能参数D3和JS调用

<script> 
    function draw(data){ 
     d3.select("body") 
       .append("div") 
       .attr("class", "chart") 
       .selectAll(".bar") 
       .data(data.cash) 
       .enter() 
       .append("div") 
       .attr("class", "bar") 
       .style("width", function(d){return d.count/100 +"px"}) 
       .style("outline", "1px solid blue") 
       .text(function(d){return Math.round(d.count)}); 
    } 
</script> 

像.NET或Java,它看起来怪我强类型语言, 在行是说

.style("width", function(d){return d.count/100 +"px"}) 

它在哪里从得到的“d”值是多少?

回答

2

匿名函数在这段代码中传递但不叫。参数d由最终调用该函数的人传递给该函数 - 在这种情况下,d3库将会。

这里有一个简单的例子,类似于你的问题的代码片段:

doSomething(function (someParameter) { 
    alert(someParameter); 
}); 

这是图书馆内的类似的功能定义:

function doSomething(someCallback) { 
    var foo = 42; 
    someCallback(foo); 
}