分配给常量变量异常

分配给常量变量异常

本文介绍了分配给常量变量异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下异常:当前值为[object Object]popup.html:1错误处理响应:TypeError:分配给常数变量. 在chrome-extension://....../popup.js:19:18试图从我的Chrome扩展程序中获取输入并在此处使用它...

I am getting the following exception: Value currently is [object Object]popup.html:1 Error handling response: TypeError: Assignment to constant variable. at chrome-extension://....../popup.js:19:18Trying to get the input from my Chrome extension and using it in here...

const settings =    {
                        name: "",
                        email: "",
                        tel: "",
                        cc_number: "",
                        cc_exp: "",
                        cc_csc: "",
                        address_line1: "",
                        address_level2: "",
                        address_level1: "",
                        postal_code: ""
                    }


function getSettingsfromStorage() {
    chrome.storage.local.get(['settings'], function(result) {
        console.log('im getter');
        console.log('Value currently is ' + result); //Exception
        settings = result; //Exception
    });
}

function saveSettingstoStorage() {
    chrome.storage.local.set({'settings': settings}, function() {
        console.log('Value is set to ' + settings);
    });
}

function getSettingsfromPopup() {
    for(let setting in settings) {
        console.log(setting);
        settings.setting = document.getElementById(setting).value;
        console.log(setting);
    }
}

function saveSettingstoPopup() {
    for(let setting in settings) {
        console.log(setting);
        if(document.getElementById(setting)) {
            document.getElementById(setting).value = settings.setting;
        }
        console.log(setting);
    }
}

async function getFromPopUpAndSaveToStorage() {
    await getSettingsfromPopup();
    saveSettingstoStorage();
}

async function getFromStorageAndSaveToSettings() {
    await getSettingsfromStorage();
    saveSettingstoPopup();
}

document.addEventListener('DOMContentLoaded', () => {
    //settings.fullName = document.getElementById(name).value;
    getFromStorageAndSaveToSettings();
    document.getElementById("saveButton").addEventListener("click", getFromPopUpAndSaveToStorage());
})

推荐答案

使用let代替const.

Use let instead of const.

您不能重新分配声明为const的变量,如果它是一个对象,则可以更改其属性.如果它是一个数组,则可以更改其值,但是仍然不能重新分配它.

You can't reassign a variable which declared as const, if it is an object, you can change its properties. If it is an array, you can change its values, but still you can't reassign it.

这篇关于分配给常量变量异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 18:13