在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。如果是這種情況,您可以更改語言級別。 –