why is my simple oop js init function is not firing?

i'm trying to learn javascript oop. here a simple init function is not firing, but is not getting an error either.

is this bad form?, how should this be done?, thanks

<script>
myGame = {}
myGame.dice = [1,2,3,4,5,6];

myGame.init = (function(){
//hide stuff
alert('test'); //not firing
$('#roll-dice-btn-2').hide();
}); 
</script>

Answers


You are not invoking your init function.

<script>
var myGame = {};
myGame.dice = [1,2,3,4,5,6];

myGame.init = (function(){
    //hide stuff
    alert('test'); //not firing
    $('#roll-dice-btn-2').hide();
});

myGame.init();

</script>

Note I've added an inline call to init and also added a semi-colon and a var statement for myGame.


You have to invoke the init function

<script>
  var myGame = {};
  myGame.dice = [1, 2, 3, 4, 5,6];

  myGame.init = function(){
    //hide stuff
    alert('test'); //not firing
    $('#roll-dice-btn-2').hide();
  }; 
  $(document).ready(function(){   //All that needs DOM (html) has to be inside this
     myGame.init();           //Here we're invoking the init method
  });
</script>

Cheers, from La Paz, Bolivia


Need Your Help

QComboBox dynamic list of items

c++ qt qcombobox

I have two QTableWidgets. Let's call them Tab1 and Tab2. One column of Tab1 contains QComboBoxes where their items are rows from Tab2. When elements of Tab2 are modified (added/removed/edited) I wa...

How to copy a single view with all the data on it in sql server from one database to another database?

sql-server database view copy

I am looking for some idea if we can generate a script for just one view and run that on the another database to create that view with its datas intact. Please help, thank you