And thank you in advance for your help. I've written a pretty standard Modal class in Vanilla JavaScript, and I am wondering why when I call the myModal.showModal
method, we always call it on the last instance of the Modal.
Here is the code for my Modal:
(function() {
/**
* Creates a Modal dialog. The user should call `removeModal` when they are
* finished using it.
* @param {boolean} defaultShow whether or not the modal is shown by default
* @param {HTMLElement} bodyContent the body of the modal
* @param {HTMLButtonElement | undefined} footerContent footer of the modal
*/
function Modal(defaultShow, initialBodyContent, initialFooterContent) {
let isActive = false;
// Create the modal and append it to the dom
let containerElement = document.createElement("div");
containerElement.className = "modal-container";
containerElement.id = Math.random().toString();
let modalBody = document.createElement("div");
modalBody.className = "modal-body";
if (initialBodyContent) {
modalBody.appendChild(initialBodyContent);
}
containerElement.appendChild(modalBody);
let modalFooter = document.createElement("div");
modalFooter.className = "modal-footer";
if (initialFooterContent) {
modalFooter.appendChild(initialFooterContent);
}
containerElement.appendChild(modalFooter);
document.body.appendChild(containerElement);
/**
* Shows the modal with new content, optionally
* @param {HTMLElement | undefined} newBodyContent replacement body element
* @param {HTMLElement | undefined} newFooterContent replacement footer element
*/
function showModal(newBodyContent, newFooterContent) {
if (isActive) {
return;
}
if (newBodyContent) {
modalBody.innerHTML = "";
modalBody.appendChild(newBodyContent);
}
if (newFooterContent) {
modalFooter.innerHTML = "";
modalFooter.appendChild(newFooterContent);
}
isActive = true;
containerElement.classList.add("show");
containerElement.classList.remove("hide");
}
/**
* Hides the modal, but does not delete it from the DOM
*/
function hideModal() {
isActive = false;
containerElement.classList.add("hide");
containerElement.classList.remove("show");
}
/**
* Completely removes the modal from the DOM
*/
function removeModal() {
$(containerElement).remove();
}
Modal.prototype.showModal = showModal;
Modal.prototype.hideModal = hideModal;
Modal.prototype.removeModal = removeModal;
Modal.prototype.isShowing = function() { return isActive; };
if (defaultShow) {
showModal();
} else {
hideModal();
}
}
module.exports = { Modal: Modal };
}())
And I am using it like so:
const myModal1 = new Modal(false);
const myModal2 = new Modal(false);
At this point, I see both Modals on the DOM. However, when i call myModal1.show(content, footer)
, we're calling the showModal
prototype of myModal2
, instead of myModal1
. However, if I change the prototype methods to:
function Modal() {
....
this.showModal = showModal;
this.hideModal = hideModal;
etc...
...
}
the code behaves as I expect it to. What is going on here exactly? I would expect the prototype function to capture my variables, but perhaps I'm mistaken.