消息传递中丢失了对象类型

use*_*064 3 javascript message-passing google-chrome-extension

我正在开发Chrome扩展程序,我遇到了以下问题.当某个事件发生时,我的后台页面会创建一个带有chrome.tabs.createAPI 的新选项卡(页面)并发送一个Object.

发送的Object(称为items)是一个对象列表,具有一个名为Item的特定类(原型).

这里有一些代码:

// in background.html
chrome.tabs.create({index: 0, url: "results.html"}, function(tab) {
     chrome.tabs.sendRequest(tab.id, {'items': itemsList}, function(response) {
         console.log(response.farewall);
     });
});     
Run Code Online (Sandbox Code Playgroud)

另一方面,在新创建的页面中,我收到发送的对象

// newpage.html
chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
        console.dir(request.items);
        sendResponse({});
    }                   
);
Run Code Online (Sandbox Code Playgroud)

问题是,当我收到对象列表时,newpage.html对象类型丢失了.确实console.dir()在新的中使用了background page,itemsList中的对象类型被正确报告,但是没有在收到的项目列表对象中报告newpage.html.

我可以手动background.html通过字符串手动序列化数据,并手动反序列化newpage.html但我想知道是否有更好的方法来支付这一点并防止列表中的对象类型(即Item)丢失.

ser*_*erg 6

在通过请求传递对象时,Chrome要求它为"JSON可序列化",这暗示它在传输之前被编码为JSON字符串,作为字符串传输,然后解码回来.JSON不支持函数序列化.