2017-09-13 53 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);