更正 JavaScript 6 类型以用于 c# 等效结构
Correct JavaScript 6 type to use for c# equivalent of struct
经过多年主要使用 C# 工作,我仍然习惯使用 JavaScript。
我有一个要实例化的对象。该对象始终具有相同的 7 个字段。它用于轻松地将一组 7 个数字传递给各种函数。
var myObj = {
4: 0,
6: 0,
8: 0,
10: 0,
12: 0,
20: 0,
100: 0
};
在 EMCAScript 6 中,已经定义了 class
es,但是从我在网上发现的内容来看,class
es 似乎主要用于在 class
,类似于 EMCAScript 5 function
的 prototype
,并且不用于定义字段。
执行此操作的正确方法是什么?
我现在的想法是:
var myObj = function () {
this[4] = 0;
this[6] = 0;
this[8] = 0;
this[10] = 0;
this[12] = 0;
this[20] = 0;
this[100] = 0;
};
var obj = new myObj();
我不认为这是一个见仁见智的问题。在大多数语言中,有一种正确的方法来做到这一点。
在 C# 中执行此操作的方法是
struct MyObj {
public int Fours { get; set; }
public int Sixes { get; set; }
public int Eights { get; set; }
public int Tens { get; set; }
public int Twelves { get; set; }
public int Twenties { get; set; }
public int Hundreds { get; set; }
}
要在 javascript 中创建一个类似于您指定的结构并能够重用该对象的对象,请尝试创建一个 returns 对象模板的函数:
function createMyObj() {
return {
"4": 0,
"6": 0,
"8": 0,
"10": 0,
"12": 0,
"20": 0,
"100": 0
};
}
javascript中有很多不同的创作模式,就看你的需要了。您需要对象层次结构吗?然后使用原型模式。您是否需要一个接受默认参数的对象创建,有点像构造函数?然后使用类似于我在上面指定的实现,但将其修改为接受 defaultValues 对象,该对象包含您需要指定的部分或全部属性。
"best" 方法非常主观。您会在这里和整个网络上看到不同的答案。查看您的 C# 示例,如果您的目标是以面向对象的方式复制类似于 class 的内容,这可能是一个很好的起点
function MyObj () {
this.Fours = 0;
this.Sixes = 0;
this.Eights = 0;
this.Tens = 0;
this.Twelves = 0;
this.Twenties = 0;
this.Hundreds = 0;
this.RollDice = function() {
console.log('here is a function')
}
};
示例用法
var myObj = new MyObj();
console.log(myObj); // -- MyObj {Fours: 0, Sixes: 0, etc...
myObj.RollDice(); // -- here is a function
JSFiddle Link
经过多年主要使用 C# 工作,我仍然习惯使用 JavaScript。
我有一个要实例化的对象。该对象始终具有相同的 7 个字段。它用于轻松地将一组 7 个数字传递给各种函数。
var myObj = {
4: 0,
6: 0,
8: 0,
10: 0,
12: 0,
20: 0,
100: 0
};
在 EMCAScript 6 中,已经定义了 class
es,但是从我在网上发现的内容来看,class
es 似乎主要用于在 class
,类似于 EMCAScript 5 function
的 prototype
,并且不用于定义字段。
执行此操作的正确方法是什么?
我现在的想法是:
var myObj = function () {
this[4] = 0;
this[6] = 0;
this[8] = 0;
this[10] = 0;
this[12] = 0;
this[20] = 0;
this[100] = 0;
};
var obj = new myObj();
我不认为这是一个见仁见智的问题。在大多数语言中,有一种正确的方法来做到这一点。
在 C# 中执行此操作的方法是
struct MyObj {
public int Fours { get; set; }
public int Sixes { get; set; }
public int Eights { get; set; }
public int Tens { get; set; }
public int Twelves { get; set; }
public int Twenties { get; set; }
public int Hundreds { get; set; }
}
要在 javascript 中创建一个类似于您指定的结构并能够重用该对象的对象,请尝试创建一个 returns 对象模板的函数:
function createMyObj() {
return {
"4": 0,
"6": 0,
"8": 0,
"10": 0,
"12": 0,
"20": 0,
"100": 0
};
}
javascript中有很多不同的创作模式,就看你的需要了。您需要对象层次结构吗?然后使用原型模式。您是否需要一个接受默认参数的对象创建,有点像构造函数?然后使用类似于我在上面指定的实现,但将其修改为接受 defaultValues 对象,该对象包含您需要指定的部分或全部属性。
"best" 方法非常主观。您会在这里和整个网络上看到不同的答案。查看您的 C# 示例,如果您的目标是以面向对象的方式复制类似于 class 的内容,这可能是一个很好的起点
function MyObj () {
this.Fours = 0;
this.Sixes = 0;
this.Eights = 0;
this.Tens = 0;
this.Twelves = 0;
this.Twenties = 0;
this.Hundreds = 0;
this.RollDice = function() {
console.log('here is a function')
}
};
示例用法
var myObj = new MyObj();
console.log(myObj); // -- MyObj {Fours: 0, Sixes: 0, etc...
myObj.RollDice(); // -- here is a function