Why is this code indicating that my object's properties are undefined?
document.getElementById("mkObj").addEventListener('click', () => {
var globalDragon = mkObj(document.getElementById("cn").value, parseInt(document.getElementById("ch").value), document.getElementById("cl").value);
document.getElementById("hit").addEventListener('click', (globalDragon) => {
hit(globalDragon, 25);
printObjDetails(globalDragon);
});
document.getElementById("details").addEventListener('click', (globalDragon) => {
let dragon_here = globalDragon;
printObjDetails(dragon_here);
});
});
Why can't I just make the Dragon object by clicking the mkObj button and then click the hit button or details button and have it show the object's state? Why is it showing that globalDragon.name is undefined?
I had the code looking like this before and it did not work either so I just make everything be within the mkObj click scope, thinking that that would solve the issue but it didn't:
document.getElementById("mkObj").addEventListener('click', ()=>{
var globalDragon = mkObj( document.getElementById("cn").value, parseInt( document.getElementById("ch").value), document.getElementById("cl").value );
});
document.getElementById("hit").addEventListener('click', (globalDragon)=>{
hit(globalDragon, 25);
printObjDetails(globalDragon);
});
document.getElementById("details").addEventListener('click', (globalDragon)=>{
let dragon_here = globalDragon;
printObjDetails(dragon_here);
});
.addEventListener()call does not remove already-added listeners, so when a "click" happens all added listeners will run.varvariable in the scope of that click listener -globalDragondoesn't exist outside of that function and will beundefinedin your other click listenersglobalDragonis redefined as a parameter. So, within the listener,globalDragonis an event object.addEventListener. You can't arbitrarily change the callback arguments to something else. Lots of issues to sort out in this code