2013-04-30 52 views
-1

空我有一个类在JS与现场Javascript类成员变成在Firefox

Widget = function() 
{ 
    this.Attributes = []; // key=value 
} 

和从Widget

BusinessStatisticWidget = function() 
{ 
    // some code 
}; 

BusinessStatisticWidget.prototype = new Widget(); 

iherited另一类在初始化阶段,我已经分配了该属性字段具有值(只有一次),并在某个点Atttibutes字段变为空:

BusinessStatisticWidget.prototype.SetEventsOnControls = function() 
{ 
    var dropDown = document.getElementById(this.DropDownName + this.type + "Id"); 

    var _this = this; // **Not empty here** 
    dropDown.addEventListener("change", function (event) 
    { 
      // **Not empty even here** 
     _this.CalculateAndSetTimeRangeForTimeSpan(event.target.value); 
    }, false); 
} 
BusinessStatisticWidget.prototype.CalculateAndSetTimeRangeForTimeSpan = function (val) 
{ 

// **Empty here** 
    if (this.Attributes["fromDate"].value != '' && this.Attributes["toDate"].value != '') 
    {} 
} 

上面的代码在Chrome和IE10(我的意思是数组不为空)的罚款,但在Firefox不工作(20.0.1)

由于数组是空的,我得到TypeError: this.Attributes.fromDate is undefined. ,我不知道为什么它是空的,如何解决这个问题。

+0

TypeError:this.Attributes.fromDate未定义。就像我说的 - 这个。属性是空的,我不知道为什么以及如何解决这个问题 – 2013-04-30 12:10:49

+0

你的代码中缺少几个右花括号。 – 2013-04-30 12:12:28

回答

1

有多个问题与您的代码:

  1. 不要使用任意键,值对数组。数组只能使用数字键。
  2. 每个实例将共享相同的Attributes数组。这通常不是理想的行为。

解决方案:

  1. 使用对象来代替。
  2. 正确设置继承并在子构造函数中调用父构造函数。

代码:

Widget = function() { 
    this.Attributes = {}; // use an pbject 
}; 


var BusinessStatisticWidget = function() { 
    // call parent constructor 
    Widget.call(this); 
    // some code 
}; 

// set up inheritance 
BusinessStatisticWidget.prototype = Object.create(Widget.prototype); 

的更多信息(和填充工具)约Object.create


现在,我不知道是否能解决你的问题,但它使你的代码至少更正确,从而找到问题变得更容易。我建议去learn how to debug JavaScript