1

How i can call function js in enum using javascript or jquery

<script>

    var alertType = {
        save: showAlertType('Save'),
        delete: showAlertType('Delete'),
    };

    function showAlertType(_alertType) {
        if (_alertType == 'Save') {
            alert(alertType.save);
        } else {
            alert(alertType.save);
        }
    };

    alertType.save;

</script>

now after you see that code, i can call function using enum ?!

Mahmod Abu Jehad
  • 167
  • 2
  • 14

1 Answers1

2

You are only getting the reference of the function but not invoking it.

alertType.save; should be alertType.save();

Also the way you defined the values in the object, you will be invoking the function immediately when the object is initialized. Instead that should be a function which gets invoked sometime in the future.

var alertType = {
  save: function() { showAlertType('Save') },
  delete: function() { showAlertType('Delete') },
};

function showAlertType(_alertType) {
  if (_alertType == 'Save') {
    alert('Save function fired');
  } else {
    alert('Some other function fired');
  }
};

alertType.save();
Sushanth --
  • 55,259
  • 9
  • 66
  • 105
  • @MahmodAbuJehad Can you explain on what references you are trying to find ? – Sushanth -- Sep 28 '18 at 19:24
  • i need to more knowlage about javascript and typescript and my big question now what is differante between * var e={ save : () => ... * var e={ save : function() { ... } – Mahmod Abu Jehad Sep 28 '18 at 19:27
  • The later are called as arrow functions which is a `ES6` feature. https://stackoverflow.com/questions/34361379/arrow-function-vs-function-declaration-expressions-are-they-equivalent-exch – Sushanth -- Sep 28 '18 at 19:38