在C#中的上下文阵列,增加新的价值观是很容易的和非混乱的事,作为一个例子,这是我在C#中通常做的:如何创建JS
namespace Sup
{
public class Pizza
{
public List<Pepperoni> PepperoniList { get; set; }
public List<Cheese> CheeseList { get; set; }
public List<Crust> CrustList { get; set; }
public List<Sauce> SauceList { get; set; }
}
public class Pepperoni
{
public string cost { get; set; }
public string quantity { get; set; }
}
public class Cheese
{
public string cost { get; set; }
public string quantity { get; set; }
}
public class Crust
{
public string cost { get; set; }
public string quantity { get; set; }
}
public class Sauce
{
public string cost { get; set; }
public string quantity { get; set; }
}
public class Program
{
static void Main(string[] args)
{
Pizza p = new Pizza()
{
PepperoniList = new List<Pepperoni>(),
CheeseList = new List<Cheese>(),
CrustList = new List<Crust>(),
SauceList = new List<Sauce>()
};
p.PepperoniList.Add(new Pepperoni() {cost = "5.00", quantity = "1"});
p.CheeseList.Add(new Cheese() {cost = "", quantity = ""});
p.CrustList.Add(new Crust() {cost = "", quantity = ""});
p.SauceList.Add(new Sauce() {cost = "", quantity = ""});
Console.WriteLine(p.PepperoniList[0].cost);
}
}
}
,你可以看看我什么时候为我的课程添加新的值,我不会再看看即时消息的内容,很容易添加新值并显示它们。
然而,在JS又是另一回事,这是林目前在JS工作:
var pepperoni= [];
pepperoni.push([["cost"], ["quantity"]]);
console.log(pepperoni[0][0]);
正如你可以看到这种形式的加入/显示值看起来不容易阅读,并与合作,我需要类似c#的例子,我该怎么办?
我使用此代码出现错误,它说'ECMAScript 2015功能。您当前的语言级别为:ECMAScript 5' – eBay
@eBay'class'语法在ES6(ES2015)中引入。您可以使用ES5语法创建类,也可以使用转译器(推荐)。使用Google搜索提示您正在使用ReSharper。如果是这种情况,您可以更改语言级别。 –