【问题标题】:How can I add an object to an object?如何将对象添加到对象?
【发布时间】:2014-09-14 16:30:47
【问题描述】:
var fillets = {};

function fillet_change(thumb, cid, sku, width, selected_matte)
{
    switch(selected_matte)
    {
      case "fillet_matte_layer_bottom": 
        moulding_matte_canvas_width[i] = $("#opening_" + i).width();
        moulding_matte_canvas_height[i] = $("#opening_" + i).height();
        index = 2;
        break;

      case "fillet_matte_layer_middle": 
        moulding_matte_canvas_width[i] = $("#opening_" + i).width() + 10;
        moulding_matte_canvas_height[i] = $("#opening_" + i).height() + 10;
        index = 1;
        break;

      case "fillet_matte_layer_top": 
        if (mattes_default_selected == true)
        {
          moulding_matte_canvas_width[i] = $("#opening_" + i).width() + 22;
          moulding_matte_canvas_height[i] = $("#opening_" + i).height() + 22;
        }
        else
        {
          moulding_matte_canvas_width[i] = $("#opening_" + i).width();
          moulding_matte_canvas_height[i] = $("#opening_" + i).height();
        }
        index = 0;
        break;
    }

    var fillet = {
      index: index,
      imgsrc: thumb,
      width: width,
      cid: cid,
      sku: sku
    };
    fillets[index] = fillet;
}

但是我收到一个错误:未捕获的类型错误:无法设置未定义的属性“2”

所以我的问题是如何将圆角对象添加到另一个对象或数组中(没关系)?

【问题讨论】:

  • “index”的值从何而来?函数是怎么调用的?
  • 我看不到 index 的设置位置。会不会是这个问题?
  • 索引设置在上面,取决于另一个值。
  • @AllisonC 您需要提供最少的代码来重现问题。你忽略了这样做。
  • "索引设置在上面,取决于另一个值。" - 那么线路在哪里?我没看到.. index: index,

标签: javascript object


【解决方案1】:

添加方法就像添加到队列中一样。

var fruits = [{size: 3, color: 'yellow'}, {size: 5, color: 'green'}];
fruits.push({size:9, color: 'black'});
console.dir(fruits);

必须是数组[3]。

0:对象 颜色:“黄色” 尺寸:3 原型:对象

1:对象 颜色:“绿色” 尺寸:5 原型:对象

2:对象 颜色:黑色” 尺寸:9 原型:对象

【讨论】:

  • 我试过了,得到:未捕获的类型错误:无法读取未定义的属性 'push'
  • 请在 Chrome 下打开你的控制台 Ctrl + Shift + J 并从上面写下 3 行。
  • 不一定是数组,也可以是类数组{3:"foo"}
【解决方案2】:

尝试添加索引变量

function fillet_change(thumb, cid, sku, width, selected_matte)
{
    var index;

    switch(selected_matte)

【讨论】:

    【解决方案3】:

    “Cannot set property '2' of undefined”表示你调用variable[2] = 'something';,变量未定义。

    这可能发生在该字符串中: moulding_matte_canvas_width[i] = $("#opening_" + i).width(); moulding_matte_canvas_height[i] = $("#opening_" + i).height();

    在您的代码中,我看不到您在哪里定义 moulding_matte_canvas_width, moulding_matte_canvas_height 变量。您确定此变量已定义且此数组的长度 >= 3?

    【讨论】:

      【解决方案4】:

      原来我在其他地方定义了圆角。所以我更改了变量的名称并且它起作用了:

      var fillet_array = [];

      function fillet_change(thumb, cid, sku, width, selected_matte)
      {
        if (typeof(selected_matte) === "undefined")
        {
          selected_matte = fillet_selected_matte;
          //console.log("fillet_selected_matte: "+ fillet_selected_matte);
        }
        if (typeof(width) === "undefined")
        {
          width = .31;
        }
        if (mattes_selected_type == 15) //letter mattes
        {
          alert("Fillets can only be applied to rectangular openings. Your current layout does not contain any rectangular openings, fillet was not applied");
          return;
        }
        var img = new Image();
        img.src =  SITE_URL + '/system/components/compimg/' + thumb + '/pattern';
        img.onload = function() 
        {
          img.width = width * ppi; 
          img.height = width * ppi; 
          $( document ).ready(function() 
          {
            $("div[id^='opening_']").each(function(i) //for each div that has an id of opening_(number) 
            {
              //Set the values in the array with the width and height
              switch(selected_matte)
              {
                case "fillet_matte_layer_bottom": 
                  moulding_matte_canvas_width[i] = $("#opening_" + i).width();
                  moulding_matte_canvas_height[i] = $("#opening_" + i).height();
                  index = 2;
                  break;
      
                case "fillet_matte_layer_middle": 
                  moulding_matte_canvas_width[i] = $("#opening_" + i).width() + 10;
                  moulding_matte_canvas_height[i] = $("#opening_" + i).height() + 10;
                  index = 1;
                  break;
      
                case "fillet_matte_layer_top": 
                  if (mattes_default_selected == true)
                  {
                    moulding_matte_canvas_width[i] = $("#opening_" + i).width() + 22;
                    moulding_matte_canvas_height[i] = $("#opening_" + i).height() + 22;
                  }
                  else
                  {
                    moulding_matte_canvas_width[i] = $("#opening_" + i).width();
                    moulding_matte_canvas_height[i] = $("#opening_" + i).height();
                  }
                  index = 0;
                  break;
              }
              var fillet_xml = "<imgsrc>" + thumb + "</imgsrc><width>" + width + "</width><cid>" + cid + "</cid><sku>" + sku + "</sku>";
              var fillet = {
                index: index,
                imgsrc: thumb,
                width: width,
                cid: cid,
                sku: sku
              };
              fillet_array.push(fillet);
              mattes_mattes_xml = mattes_get_mattes_xml().replace("<fillet index='" + index + "'></fillet>", "<fillet index='" + index + "'>" + fillet_xml + "</fillet>");
              common_get_order_xml();
              moulding_draw(img, "fillet", selected_matte); //Call the moulding_draw function, which draws the fillet on the canvas
            });
          });
        };
      }
      

      【讨论】:

        猜你喜欢
        • 2023-01-19
        • 2021-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-13
        • 2010-12-17
        • 1970-01-01
        • 2018-09-06
        相关资源
        最近更新 更多