Hello Everyone! Can somebody please help me to understand what I am doing wrong with this Game Task.
I have 3 diferent contents in this object:
1.- Start of the game
2.- End of the Game to show results
3.- the Game board where the users can play…
Now, what I want to do and it does not work is:
in my Literal Object i have the Key with the name displayGame (like a constructor). Problem: I have the key with the name: gameHandler, it should display the key: displayBoard content when the user click on the class button - Star Game which is in Keys: displayStart and displayEnd.
Here is my Code:
this.gameHandler() // run function after you render `gameStart`
button = document.getElementsByClassName('button') // is calling a HTMLcollection
var button = document.getElementsByClassName('button')[0] // should set its index
var button = document.querySelector('.button') // or use querySelector
button.onclick = function() {
this.displayBoard(); // `this` value doesn't refer to the originating context
}.bind(this) // bind your object UserInterface or use es6 arrow function
i made a fiddle of your project.
i made two buttons which logs this values for you to compare.
1 which doesn’t use .bind() and another which is bound to its parent object.
open your browser console and check logs whenever button’s clicked.
I just call the GameUI object and their Object Key : GameUI.displayBoard() to display displayBoard
I don’t know if this solution is ok… but it works. Would you recommend this solution as a one of the alternative solutions?