提前感谢你的帮助。我在Vanilla JavaScript中编写了一个相当标准的Modal类,我想知道为什么当我调用myModal.showModal方法时,我们总是在Modal的最后一个实例上调用它。
下面是我的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 };
}())我是这样用它的:
const myModal1 = new Modal(false);
const myModal2 = new Modal(false);在这一点上,我看到了DOM上的两个Modals。但是,当我调用myModal1.show(content, footer)时,我们调用的是myModal2的showModal原型,而不是myModal1。但是,如果我将原型方法更改为:
function Modal() {
....
this.showModal = showModal;
this.hideModal = hideModal;
etc...
...
}代码的行为与我所期望的一样。到底是怎么回事?我希望原型函数能够捕获我的变量,但我可能弄错了。
发布于 2020-08-11 17:30:14
Modal.prototype对所有Modal实例都具有相同的值,因此所有Modal实例的showModal方法引用最后一个实例的showModal方法。
例如:
function A () {
const randomNumber = Math.floor(Math.random() * 100);
A.prototype.id = randomNumber;
console.log("creating instance with id: " + randomNumber);
}
const a1 = new A()
const a2 = new A()
console.log("Current ID of a1: " + a1.id);
console.log("Current ID of a2: " + a2.id);
console.log(a1.__proto__ === a2.__proto__); // Will return "true" because it's the same object
您可以在文献资料上阅读更多内容。
https://stackoverflow.com/questions/63363210
复制相似问题