Create a JavaScript array of a function pointer without calling it

I have the code below. I would like to have an array (buttons) with one element pointing to a (closeFlag) function.

<script type="text/javascript"> var closeFlag = new function() { alert('Clicked'); } var buttons = { 'OK': closeFlag } </script> 

However, when the page loads, a pop-up warning immediately appears. When the array is constructed, instead of using it as a pointer, JavaScript calls my function. What for? What mistake, misconception do I have?

+4
source share
1 answer

The new keyword, you won’t need it.

 <script type="text/javascript"> var closeFlag = function() { alert('Clicked'); } var buttons = { 'OK': closeFlag } </script> 

What happens in your code is that it creates an anonymous function and then assigns the result ( this ) to closeFlag .

+12
source

Source: https://habr.com/ru/post/1307196/


All Articles