dispatchMessage 和关联数组

发布于 2024-10-06 10:36:32 字数 904 浏览 0 评论 0原文

我在将关联数组传递给注入脚本时遇到问题。

Global.html

  var settings = new Array();

  settings["accountID"] = safari.extension.settings.getItem("accountID");
  settings["accountName"] = safari.extension.settings.getItem("accountName");
  settings["accountEmail"] = safari.extension.settings.getItem("accountEmail");

            safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("settingsArray", settings);

script.js

 switch (msgEvent.name) {
      case "settingsArray":
           var settings = new Array();
           settings = msgEvent.message;
           console.log("accountID: " + settings["accountID"]);

           break;

当我使用“普通”数组时,它工作得很好!

但是当提供关联数组时,我总是得到调用时显示“未定义”,例如。 settings["accountID"]

有人知道出了什么问题吗?

I have a problem delivering assiciative arrays to an injected script.

Global.html

  var settings = new Array();

  settings["accountID"] = safari.extension.settings.getItem("accountID");
  settings["accountName"] = safari.extension.settings.getItem("accountName");
  settings["accountEmail"] = safari.extension.settings.getItem("accountEmail");

            safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("settingsArray", settings);

script.js

 switch (msgEvent.name) {
      case "settingsArray":
           var settings = new Array();
           settings = msgEvent.message;
           console.log("accountID: " + settings["accountID"]);

           break;

When I do it with "normal" arrays, it works fine!

But when delivering associative arrays, I always get "undefined" when calling eg. settings["accountID"]

Does anyone have an idea what's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白日梦 2024-10-13 10:36:32
  1. 当你应该使用对象时,你却使用了数组。

    var settings = new Array(); // 错误的
    变量设置 = {}; // 正确(比“new Object()”更好)
    
  2. 您没有必要使用字符串形式的属性访问。

    设置["accountID"] = …; // 可以,但是输入太多
    设置.accountID = ...; // 完全相同的功能
    

    如果属性名称不是有效的 JavaScript 标识符,则在获取/设置属性值时只需使用方括号表示法(例如 foo["holy!*#$! it Works"] = true)或者如果您需要从变量构造属性名称(例如foo["account"+n] = "active";)。

  3. 您正在创建新对象然后把它们扔掉。

     var settings = new Array(); // 创建一个由变量引用的新数组
     设置= msgEvent.message; // 丢弃数组并更改变量
                                  // 引用一个新对象
    
  1. You're using arrays when you should be using objects.

    var settings = new Array();  // Wrong
    var settings = {};           // Right (and better than "new Object()")
    
  2. You are unnecessarily using the string form of property access.

    settings["accountID"] = …;   // Works, but too much typing
    settings.accountID = …;      // Exact same functionality
    

    You only need to use the bracket notation when getting/setting property values if the property name is not a valid JavaScript identifier (e.g. foo["holy!*#$! it works"] = true) or if you need to construct the property name from a variable (e.g. foo["account"+n] = "active";).

  3. You are creating new objects and then throwing them away.

     var settings = new Array();  // Makes a new array referenced by a variable
     settings = msgEvent.message; // Discards the array and changes the variable
                                  // to reference a new object
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文