Javascript 将隐藏/显示方法传递给另一个方法
我在代码中以两种方式调用对象方法:Javascript 将隐藏/显示方法传递给另一个方法,javascript,jquery,string,methods,Javascript,Jquery,String,Methods,我在代码中以两种方式调用对象方法: this.discover.updateVisuals(i'show') 或 我以字符串的形式传递隐藏和显示条件,以便稍后进行计算并用作方法。请注意条件:如果(效果=='show/hide') 但是,代码似乎重复了,我想知道是否有一种方法可以将hide或show作为方法传递给该方法,并在需要时将其应用于数组。我试过这样的方法: this.discover.updateVisuals(i).show您可以使用[括号]符号通过方法属性的(字符串)名称访问该方法属性
this.discover.updateVisuals(i'show')
或
我以字符串的形式传递隐藏和显示条件,以便稍后进行计算并用作方法。请注意条件:如果(效果=='show/hide')
但是,代码似乎重复了,我想知道是否有一种方法可以将hide或show作为方法传递给该方法,并在需要时将其应用于数组。我试过这样的方法:
this.discover.updateVisuals(i).show您可以使用[括号]符号通过方法属性的(字符串)名称访问该方法属性
updateVisuals: function (time, effect) {
// Check if parameter exists and property can be read
if (this.breakpointsMap && typeof this.breakpointsMap[checkTime] !== "undefined") {
var k = this.breakpointsMap[checkTime].length;
while (k--) {
try {
this.breakpointsMap[checkTime][k][effect]();
} catch (err) {}
}
}
}
您可以使用[括号]表示法通过方法属性的(字符串)名称访问该方法属性
updateVisuals: function (time, effect) {
// Check if parameter exists and property can be read
if (this.breakpointsMap && typeof this.breakpointsMap[checkTime] !== "undefined") {
var k = this.breakpointsMap[checkTime].length;
while (k--) {
try {
this.breakpointsMap[checkTime][k][effect]();
} catch (err) {}
}
}
}
有很多方法可以用来简化此过程,以下是一些方法:
updateVisuals: function (time, effect) {
if (this.breakpointsMap && typeof this.breakpointsMap[checkTime] !== "undefined") {
this.breakpointsMap[checkTime].forEach(e => e[effect]());
}
}
或返回数组:
updateVisuals: function (time, effect) {
if (this.breakpointsMap && typeof this.breakpointsMap[checkTime] !== "undefined") {
return this.breakpointsMap[checkTime];
}else{
return [];
}
}
this.reveal.updateVisuals(i).forEach(e => e.show());
有很多方法可以用来简化此过程,以下是一些方法:
updateVisuals: function (time, effect) {
if (this.breakpointsMap && typeof this.breakpointsMap[checkTime] !== "undefined") {
this.breakpointsMap[checkTime].forEach(e => e[effect]());
}
}
或返回数组:
updateVisuals: function (time, effect) {
if (this.breakpointsMap && typeof this.breakpointsMap[checkTime] !== "undefined") {
return this.breakpointsMap[checkTime];
}else{
return [];
}
}
this.reveal.updateVisuals(i).forEach(e => e.show());
如果您正在使用Es6,您可以执行以下操作:
function updateVisuals (time, effect) {
// Check if parameter exists and property can be read
if (this.breakpointsMap && typeof this.breakpointsMap[checkTime] !== "undefined") {
let execFn= (arrindex) => breakpointsMap[checkTime][arrindex].show();
if (effect === 'hide')
{
execFn = (arrindex) => breakpointsMap[checkTime][arrindex].hide();
}
// display the items that were fast forwarded
var k = this.breakpointsMap[checkTime].length;
while (k--) {
try {
execFn(k);
} catch (err) {}
}
}
}
我假设var checkTime是全局的或处于关闭状态。如果您使用的是版本较低的tan es6,则可以使用execFn=function(arrindex){…},然后在调用方法after时绑定此参数。如果您使用的是es6,则可以执行以下操作:
function updateVisuals (time, effect) {
// Check if parameter exists and property can be read
if (this.breakpointsMap && typeof this.breakpointsMap[checkTime] !== "undefined") {
let execFn= (arrindex) => breakpointsMap[checkTime][arrindex].show();
if (effect === 'hide')
{
execFn = (arrindex) => breakpointsMap[checkTime][arrindex].hide();
}
// display the items that were fast forwarded
var k = this.breakpointsMap[checkTime].length;
while (k--) {
try {
execFn(k);
} catch (err) {}
}
}
}
我假设var checkTime是全局的或处于关闭状态。如果您使用的是version lower tan es6,那么您可以使用execFn=function(arrindex){…},然后在调用方法after时绑定此参数。您可以使用类似这样的方法。
this.breakpointsMap[checkTime][k][effect]()
您可以使用类似这样的东西this.breakpoints映射[checkTime][k][effect]()代码>