JavaScript中Var类型的新实例

时间:2015-02-26 08:34:16

标签: javascript

如何在JavaScript中创建var类型变量的新实例?

例如

var UserControl = { Id: '', Location: { Left: 0, Top: 0, Width: 0, Height: 0 }, Contents: '', Name: '' };

我想将此UserControl存储在JavaScript Array中。如下所示。

UserControl.Id = '1';
UserControl.Location.Left = offset.left;
UserControl.Location.Top = offset.top;
UserControl.Location.Width = offset.left + area.width();
UserControl.Location.Height = offset.top + area.height();
UserControlCollection.push(UserControl);

但UserControl会覆盖以前的条目,因为 UserControl 不会创建新实例。

这是什么指针?

1 个答案:

答案 0 :(得分:2)

您可以尝试使用函数作为构造函数,就像这样

function UserControl() {
  this.Id = '';
  this.Location = {
    Left: 0, Top: 0, Width: 0, Height: 0
  };
  this.Contents = '';
  this.Name = '';
}

Example