gpt4 book ai didi

javascript - JavaScript 中 Var 类型的新实例

转载 作者:行者123 更新时间:2023-11-29 21:51:52 24 4
gpt4 key购买 nike

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

例如

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

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

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 不会创建新实例。

有什么指示吗?

最佳答案

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

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

Example

关于javascript - JavaScript 中 Var 类型的新实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28737680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com