如何将常用方法提取到函数中
How to extract common methods into a function
我想提取常用方法链以防止复制粘贴相同的代码。
如何以Javascript方式做到这一点?
源语言
if(this.get('with_coverage')){
svg.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline")
.text("With Coverage");
}
else{
svg.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline")
.text("Without Coverage");
}
预期
var apply_style = attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline");
if(this.get('with_coverage')){
svg.append("text")
.apply_style()
.text("With Coverage");
}
else{
svg.append("text")
.apply_style()
.text("Without Coverage");
}
svg.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline")
.text("With"+(this.get('with_coverage')?"":"out")+" Coverage");
魔法。
有些人不喜欢那些混在代码中的三元运算符。在这种情况下,您可以这样做:
function applyStyleBasedOnCondition(selection, actions, condition, t, f){
actions.call(selection);
(condition ? t : f).call(selection);
}
applyStyleBasedOnCondition(svg.append("text"), function(){
this
.attr({
x: width / 2,
y: 0 - (margin.top / 2),
"text-anchor": "middle"
})
.style({
"font-size": "16px",
"text-decoration": "underline"
});
}, this.get('with_coverage'), function(){
this.text("With Coverage");
}, function(){
this.text("Without Coverage");
});
链接是通过返回相同的对象来实现的。要在 svg 对象中使用apply_style
方法,您可以执行以下操作:
svg.apply_style = function(){
this.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline");
return this; //important
}
var svg = d3.select('#svg')
// 1st approach - simple and stable
var circle = function(){
return svg.append('circle')
.attr('cx', 100)
.attr('cy', 100)
.attr('r', 40)
}
circle()
.style('fill', 'green')
circle()
.style('fill', 'red')
.attr('r', 20)
// 2nd approach - more generic, but not very stable.
// !! Use it carefully !!
d3.selection.prototype.myFillMixin = function(){
return this.style('fill', '#27F')
.style('stroke', 'red')
.style('stroke-width', 4)
.style('opacity', .7)
}
svg.append('rect')
.attr('x', 200)
.attr('y', 200)
.attr('width', 100)
.attr('height', 100)
.myFillMixin()
svg.append('rect')
.attr('x', 240)
.attr('y', 240)
.myFillMixin() //No matter where to place
.attr('width', 100)
.attr('height', 100)
小提琴
相关文章:
- jQueryAJAX函数被多次调用,但数据只在第一次调用时被提取
- Javascript变量作用域:从回调函数中提取变量
- 正则表达式或函数,用于提取传递给函数的所有参数
- 如何将闭包从外部函数中提取出来,以便在不同的上下文中重用所述闭包
- 从函数中的函数中提取值
- 如何在解析html页面时从html页面中的javascript函数中提取变量的值
- 如何在JS/Ajax中每秒提取PHP函数
- 提取并调用在元素的 onclick HTML 属性中定义的 JavaScript 函数(使用 jQuery attr/p
- 从函数中提取变量
- 如何在函数中提取变量并放入另一个函数
- 使用递归和 yield 关键字提取嵌套列表的 Typescript 函数
- 正则表达式提取带有嵌套函数(javascript)的函数表示法
- 使用Javascript正则表达式从函数中提取注释
- 可以从javascript文件中提取函数
- 如何提取函数内部使用的多个方法名
- 从jQuery中的load()函数中提取一个变量以供通用
- 为什么不是't我的javascript函数从文本区域提取文本
- 无法从窗体向函数提取数据
- 在Javascript函数(Ruby on rails)中使用onclick函数提取局部函数
- 使用jQuery's filter()函数提取具有不同条件的数组