d3.json回调函数 [英] d3.json callback function
问题描述
我正在使用d3.js 3.4.13,文档说d3.json(data, callback)
的回调函数应采用callback(error, responseData)
格式,但是,我发现它实际上将数据设置为第一个参数,并且第二个参数是不必要的,所以我改用callback(responseData)
.它可以工作,但是我很困惑为什么它不能以我解释文档来描述它的方式来工作.我想念什么?
I'm using d3.js 3.4.13, and the docs say the callback function for d3.json(data, callback)
should be of the format callback(error, responseData)
, however, I found that it actually sets the data to the first parameter, and the second parameter is unnecessary, so I use callback(responseData)
instead. It works, but I'm confused why it doesn't work the same way I interpreted the docs to describe it. What am I missing?
推荐答案
快速浏览源揭示了以下功能:
A quick look at the source reveals the following function:
function d3_xhr_fixCallback(callback) {
return callback.length === 1
? function(error, request) { callback(error == null ? request : null); }
: callback;
}
D3通过.length
显式检查回调参数,如果只有一个,则将数据放入其中.
D3 explicitly checks the number of arguments to the callback through .length
and if there's only one, puts the data in it.
因此,一个参数的版本是仍然可以按预期工作的便捷版本.
So the one-argument version is a convenience version that still works as expected.
这篇关于d3.json回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!