2017-09-13 54 views
2

在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#的例子,我該怎麼辦?

回答

2

您的代碼可以直接翻譯成JavaScript:

class Pizza { 
    constructor(){ 
     this.pepperoniList = []; 
     // add more if you want 
    } 
} 

class Pepperoni { 
    constructor(cost, quantity){ 
     this.cost = cost; 
     this.quantity = quantity; 
    } 
} 

var p = new Pizza(); 
p.pepperoniList.push(new Pepperoni(5, 1)); // add a new Pepperoni object 
+0

我使用此代碼出現錯誤,它說'ECMAScript 2015功能。您當前的語言級別爲:ECMAScript 5' – eBay

+0

@eBay'class'語法在ES6(ES2015)中引入。您可以使用ES5語法創建類,也可以使用轉譯器(推薦)。使用Google搜索提示您正在使用ReSharper。如果是這種情況,您可以更改語言級別。 –

1

在Javascript解決方案中,您要添加一個包含兩個數組的數組,而不是一個對象。只需添加一個對象來獲得相同的結果在C#:

var pepperoni = []; 
pepperoni.push({ cost: 5.0, quantity: 1 }); 

console.log(pepperoni[0]); // prints { cost: 5.0, quantity: 1 } 
+0

是的,這就是我一直在尋找,謝謝! – eBay

1

也許這樣的事情?

class Ingredient { 
 
    constructor(cost, quantity) { 
 
    this.cost = cost; 
 
    this.quantity = quantity; 
 
    } 
 
} 
 

 
class Pepperoni extends Ingredient { 
 

 
} 
 

 
class Cheese extends Ingredient { 
 

 
} 
 

 
class Crust extends Ingredient { 
 

 
} 
 

 
class Sauce extends Ingredient { 
 

 
} 
 

 
class Pizza { 
 
    constructor(crust, sauce, cheese, pepperoni) { 
 
    this.crust = crust; 
 
    this.sauce = sauce; 
 
    this.cheese = cheese; 
 
    this.pepperoni = pepperoni; 
 
    } 
 
} 
 

 
const pizza = new Pizza(new Crust(5, 1), new Sauce(2, 1), new Cheese(3, 1), new Pepperoni(2, 1)); 
 
console.log(pizza);