var imgs = {
a: function(){
//...
},
b: function(){
//...
},
c: function(){
//...
}
};
var preloadImgs = function(){
imgs.a();
imgs.b();
imgs.c();
};
preloadImgs();
Is there a way to automate preloadImgs so it loads all the methods inside imgs at once, instead of me having to type every single one?
Use Object.values
Object.values( imgs ).forEach( s => s() );
Demo
var imgs = {
a: function(){
console.log("1");
},
b: function(){
console.log("2");
},
c: function(){
console.log("3");
}
};
var preloadImgs = function(){
Object.values( imgs ).forEach( s => s() );
};
preloadImgs();
Edit
If all the values in img
s are not function, then you can apply the filter first
Object.values( imgs )
.filter( s => typeof s === 'function' ) //filter out values which are function
.forEach( s => s() ); //execute those functions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With