js self executing object literal methods
by Matt Rummler
HTML
<section id="updateme">
Replace or modify this text:
</section>
JavaScript
/**
* After trying a large number of things it turns out here is what is required:
1. An anonymous function assigned to a variable. NOTE: There is another way to do this without assigning it to a variable, see the second pattern here: http://www.andismith.com/blog/2011/10/self-executing-anonymous-revealing-module-pattern/
2. Inside the anonymous function another named function must be created
3. The named function must be called after it's defined and still inside the anonymous function assigned to a variable
4. after the anonymous function the following must appear "()"
There is often a set of perenthesis surrounding the anonymous function... There may be some sort of bug that can occasionally appear without them... I would think they are being used as
UPDATE: The parenthesis are valuable if you want to do an arrow function instead of a standard function call
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
*/
var testObj =
{
testObjProperty1: 'value of testObjProperty1';
showStuff: (() =>
{
var showMessage = function(){window.alert('This is the message from showMessage');};
showMessage();
})(),
};
/**/
var showStuffAgain = function()
{
var showMessage2 = function(){window.alert('This is the message from showMessage2');};
showMessage2();
}();
//*/
var showStuffThree = function()
{
var showMessage3 = function(){document.getElementById('updateme').innerHTML = 'message from showMessage3: \"Hello!\"';};
showMessage3();
}();