将函数应用于多个对象

时间:2012-09-17 14:59:32

标签: javascript

我在下面有以下代码:

this.resize2.intrface.transform(Raphael.format("t{0},{1},S{2},{3},{4},{5}{6}",
                                    this.resize2.intrface.ox,
                                    this.resize2.intrface.oy,                                                
                                    xmove,    
                                    ymove,
                                    this.resize2.bbx,
                                    this.resize2.bby,
                                    this.resize2.intrface.scale));
this.resize2.core.transform(Raphael.format("t{0},{1},S{2},{3},{4},{5}{6}",
                                    this.resize2.intrface.ox,
                                    this.resize2.intrface.oy,                                                
                                    xmove,    
                                    ymove,
                                    this.resize2.bbx,
                                    this.resize2.bby,
                                    this.resize2.intrface.scale));

我想将转换函数应用于一行中的两个对象。这可能吗?要了解我打算做什么,请参阅下面的(不工作)示例:

$(this.resize2.intrface, this.resize2.core).transform(Raphael.format("t{0},{1},S{2},{3},{4},{5}{6}",
                                    this.resize2.intrface.ox,
                                    this.resize2.intrface.oy,                                                
                                    xmove,    
                                    ymove,
                                    this.resize2.bbx,
                                    this.resize2.bby,
                                    this.resize2.intrface.scale));

谢谢!

1 个答案:

答案 0 :(得分:0)

这是一些重复的代码重构。

function transform(factoryMethod) {
    factoryMethod(Raphael.format("t{0},{1},S{2},{3},{4},{5}{6}",
                    this.resize2.intrface.ox,
                    this.resize2.intrface.oy,
                    xmove,    
                    ymove,
                    this.resize2.bbx,
                    this.resize2.bby,
                    this.resize2.intrface.scale));
}

使用此方法调用:

transform.apply(this, [this.resize2.intrface.transform]);
transform.apply(this, [this.resize2.core.transform]);