动态访问全局未定义变量(D3 中的示例),DRY
Dynamically accessing global undefined variables (example in D3), DRY
继 之后,我想创建一种 DRY 方法来为代表英国报纸每日情绪分析的 D3 图创建 Js 变量。
这是我脚本中的一些示例代码:
var guardian,independent; // many more here
var gLine,gChart; // many more here
var iLine,iChart; // many more here
我正在将报纸特定变量存储在一个对象中:
var allObjects = { guardian : {line : gLine,chart : gChart},
independent : {line : iLine,chart : iChart}}// and so on for each newspaper
我使用如下函数分配变量:
function makeLine(name){return d3.svg.line().y(function(d) { return y(d[name]); }); }
// and so on for each newspaper attribute in AllObjects
与其一直重复自己,不如单独制作每个对象:
makeLine('guardian'); makeLine('independent'); // etc
...效果很好,我希望能够遍历所有报纸,并为所有报纸分配具有单一功能的对象,例如:
var allFunctions = {line: makeLine(),chart: makeChart()};
function make(type){
var myFunc = allFunctions.type;
for(var prop in allObjects){prop.type = myFunc(type);}
}
这样 make(line)
将分配 gLine
、iLine
等
问题在于,由于allObjects.guardian
中的变量未定义,此方法无效。
关于如何以这种方式重构有什么建议吗?
Rather than repeating myself all the time, making each object individually:
makeLine('guardian'); makeLine('independent'); // etc
...which works fine, I would like to be able to iterate over all the newspapers, and assign the objects with a single function for all newspapers
如果我没看错,你的“类似的东西”真的接近,请参阅评论:
var allFunctions = {line: makeLine, chart: makeChart};
// Note no () here ----------------^ or here --------^
// We want the reference to the function, we don't want to call it (yet)
// Assuming `type` is "line", "chart", etc.
function make(type){
// Note brackets: We want the property whose name is in the type
// variable, not a property actually called "type"
var myFunc = allFunctions[type];
// ^----^------ We want the property whose name is in
// the `type` variable, not a property
// actually *called* "type"
for (var prop in allObjects) {
allObjects[prop][type] = myFunc(prop);
// ^----^-----^----------- Brackets again as above
}
}
继
这是我脚本中的一些示例代码:
var guardian,independent; // many more here
var gLine,gChart; // many more here
var iLine,iChart; // many more here
我正在将报纸特定变量存储在一个对象中:
var allObjects = { guardian : {line : gLine,chart : gChart},
independent : {line : iLine,chart : iChart}}// and so on for each newspaper
我使用如下函数分配变量:
function makeLine(name){return d3.svg.line().y(function(d) { return y(d[name]); }); }
// and so on for each newspaper attribute in AllObjects
与其一直重复自己,不如单独制作每个对象:
makeLine('guardian'); makeLine('independent'); // etc
...效果很好,我希望能够遍历所有报纸,并为所有报纸分配具有单一功能的对象,例如:
var allFunctions = {line: makeLine(),chart: makeChart()};
function make(type){
var myFunc = allFunctions.type;
for(var prop in allObjects){prop.type = myFunc(type);}
}
这样 make(line)
将分配 gLine
、iLine
等
问题在于,由于allObjects.guardian
中的变量未定义,此方法无效。
关于如何以这种方式重构有什么建议吗?
Rather than repeating myself all the time, making each object individually:
makeLine('guardian'); makeLine('independent'); // etc
...which works fine, I would like to be able to iterate over all the newspapers, and assign the objects with a single function for all newspapers
如果我没看错,你的“类似的东西”真的接近,请参阅评论:
var allFunctions = {line: makeLine, chart: makeChart};
// Note no () here ----------------^ or here --------^
// We want the reference to the function, we don't want to call it (yet)
// Assuming `type` is "line", "chart", etc.
function make(type){
// Note brackets: We want the property whose name is in the type
// variable, not a property actually called "type"
var myFunc = allFunctions[type];
// ^----^------ We want the property whose name is in
// the `type` variable, not a property
// actually *called* "type"
for (var prop in allObjects) {
allObjects[prop][type] = myFunc(prop);
// ^----^-----^----------- Brackets again as above
}
}