首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么当我使用原型时,我的Modal代码会失败,而当我使用"this“(Vanilla JS)时,我的Modal代码会失败呢?

为什么当我使用原型时,我的Modal代码会失败,而当我使用"this“(Vanilla JS)时,我的Modal代码会失败呢?
EN

Stack Overflow用户
提问于 2020-08-11 17:16:01
回答 1查看 47关注 0票数 0

提前感谢你的帮助。我在Vanilla JavaScript中编写了一个相当标准的Modal类,我想知道为什么当我调用myModal.showModal方法时,我们总是在Modal的最后一个实例上调用它。

下面是我的Modal代码:

代码语言:javascript
复制
(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 };
}())

我是这样用它的:

代码语言:javascript
复制
const myModal1 = new Modal(false);
const myModal2 = new Modal(false);

在这一点上,我看到了DOM上的两个Modals。但是,当我调用myModal1.show(content, footer)时,我们调用的是myModal2showModal原型,而不是myModal1。但是,如果我将原型方法更改为:

代码语言:javascript
复制
function Modal() {
    ....
    this.showModal = showModal;
    this.hideModal = hideModal;
    etc...
    ...
}

代码的行为与我所期望的一样。到底是怎么回事?我希望原型函数能够捕获我的变量,但我可能弄错了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-11 17:30:14

Modal.prototype对所有Modal实例都具有相同的值,因此所有Modal实例的showModal方法引用最后一个实例的showModal方法。

例如:

代码语言:javascript
复制
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

您可以在文献资料上阅读更多内容。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63363210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档