【发布时间】:2017-10-04 12:32:33
【问题描述】:
我在代码中以两种方式调用对象方法:
this.reveal.updateVisuals(i, 'show');
或
this.reveal.updateVisuals(i, 'hide');
我将隐藏和显示条件作为字符串传递,以供稍后评估并用作方法。请注意条件:if (effect === 'show/hide')。
updateVisuals: function (time, effect) {
// Check if parameter exists and property can be read
if (this.breakpointsMap && typeof this.breakpointsMap[checkTime] !== "undefined") {
if (effect === 'show') {
// display the items that were fast forwarded
var k = this.breakpointsMap[checkTime].length;
while (k--) {
try {
this.breakpointsMap[checkTime][k].show();
} catch (err) {}
}
} else if (effect === 'hide') {
// display the items that were fast forwarded
var k = this.breakpointsMap[checkTime].length;
while (k--) {
try {
this.breakpointsMap[checkTime][k].hide();
} catch (err) {}
}
}
}
}
但是代码似乎重复了,我想知道是否有一种方法可以将 hide 或 show 作为方法传递给该方法,并在需要时将其应用于数组。我尝试过这样的事情:
this.reveal.updateVisuals(i).show
【问题讨论】:
-
你可以使用类似
this.breakpointsMap[checkTime][k][effect]();
标签: javascript jquery string methods