In PHP we have the variable functions , which basically allow us to do this among other things
class Bar
{
function __construct($foo)
{
$this->{'funcion' . $foo}();
}
private function funcionHola()
{
echo "Hola!";
}
private function funcionAdios()
{
echo "Adios!";
}
}
$barHola = new Bar('Hola');
echo "<br />";
$barAdios = new Bar('Adios');
And it will print this:
Hola!
Adios!
Is there a way to do this in javascript? I mean to dynamically call a function or method
I have this code in javascript, and in load()
I want to call the variable function
(function(window, document) {
'use strict';
var Bar = {
load: function(foo) {
// Llamar a la función variable
},
funcionHola: function() {
console.log('Hola');
},
funcionAdios: function() {
console.log('Adios');
}
};
Bar.load('Hola');
Bar.load('Adios');
}(window, window.document));