首页 > 解决方案 > es6 错误中的 Ready only 错误和 const

问题描述

我在尝试运行的此脚本中收到只读错误。

我正在尝试学习 const 和新的 es6,我不得不承认它非常简单,但在这种情况下,我不能 100% 确定这就是问题所在。

这是我的代码:

$(() => {
    const containerImage = document.querySelector( '.images' );
    const getPictures = () => {
        $.ajax({url: "https:myurl/photos.json", success: function(result) {
            const singleImage = result.photos;
            const content = "";
            content += "<div class='row'>";
            for(let i = 0; i < singleImage.length; i++){
                content.innerHTML += `<div class="col-md-4">
                <img src=${singleImage[i].image}>
                </div>`;
            };
            content += "</div>";
            containerImage.innerHTML = content;
        }});

    }
    getPictures();
});

有人注意到我的代码中有什么奇怪的地方吗?

加上控制台提及并抛出此错误:

function _readOnlyError(name) { throw new Error("\"" + name + "\" is read-only"); }

但我什至不在严格模式下。

标签: ecmascript-6constantsstrict

解决方案


不能修改常量。改变:

const content = ""; 
content += "<div class='row'>";

let content = "";
content += "<div class='row'>";

推荐阅读