如何将对象添加到对象?

时间:2014-07-23 13:07:57

标签: javascript object

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;
}

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

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

4 个答案:

答案 0 :(得分:0)

添加方法,就像添加队列一样。

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

必须是数组[3]。

0:对象 颜色:“黄色” 大小:3 proto :对象

1:对象 颜色:“绿色” 大小:5 proto :对象

2:对象 颜色:“黑色” 大小:9 proto :对象

答案 1 :(得分:0)

尝试添加索引变量

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

    switch(selected_matte)

答案 2 :(得分:0)

“无法设置未定义的属性'2'”表示您调用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?

答案 3 :(得分:0)

事实证明我在其他地方定义了鱼片。所以我改变了变量的名称,它起作用了:

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
      });
    });
  };
}