-1

我正在将一个Timer对象存储到chrome.storage.sync中。当我检索Timer对象,并尝试调用其中的一个方法,countdown(),方法不再存在:为什么检索时存储到chrome.storage.sync中的对象的方法不是函数?

let clock = new Timer(1); 
    let key = "myKey"; 
    chrome.storage.sync.set({[key]: clock}); 
    chrome.storage.sync.get(key, function(items){ 
    items[key].countdown(); 
    }); 

铬devTools说:“响应错误到storage.get:类型错误:项目[关键]。倒计时不是一个功能。“

+0

你为什么要将它存储到'storage.sync'中?这是否真的是你希望在不同的机器上同步链接到同一个配置文件?当然,这看起来并不合适。 – Makyen

回答

0

您不能直接在chrome.storage中存储函数。

StorageArea.set()的文档是相当明确的[重点煤矿]:

Primitive values such as numbers will serialize as expected. Values with a typeof "object" and "function" will typically serialize to {}, with the exception of Array (serializes as expected), Date, and Regex (serialize using their String representation).

这似乎是JSON的一个子集(JSON将序列元就好组成的对象)。但是,即使它是一个完整的JSON实现,它仍然不会存储函数:“JSON本身并不代表函数,正则表达式,日期等更复杂的数据类型。”

相关问题