Javascript 在自定义对象的javascript中创建一个数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5115810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create an array in javascript of custom objects
提问by David
Hi I need some help with javascript.
嗨,我需要一些 JavaScript 方面的帮助。
function PricingData(id,method,freq,service,price) {
this.ID=id;
this.PaymentMethod_ID=method;
this.PaymentFrequency_ID=freq;
this.Service_ID=service;
this.Price=price;
}
I need to create an array in this way.
我需要以这种方式创建一个数组。
var temp=new PricingData[]{new PricingData(1,2,3,4,5),new PricingData(1,2,3,4,5)};
but this doesn't work. I'm going to pass the data in through the server so I would prefer syntax similar to this
但这不起作用。我将通过服务器传递数据,所以我更喜欢与此类似的语法
回答by Felix Kling
Use array literal notation to create an array:
使用数组文字表示法创建数组:
var tmp = [new PricingData(1,2,3,4,5), new PricingData(1,2,3,4,5)];
For more information about arrays have a look at MDC - Array.
有关数组的更多信息,请查看MDC-Array。
回答by Ollie Edwards
just put the new object right between the square brakcets
只需将新对象放在方括号之间
var arr = [new whatever(), new whatever()];
回答by Fresh Coder
That's how to create an array of custom object by using this syntax:
这就是使用以下语法创建自定义对象数组的方法:
var myObjArr = new Array(new PricingData(1,2,3,4,5), new PricingData(1,2,3,4,5));
var myObjArr = new Array(new PricingData(1,2,3,4,5), new PricingData(1,2,3,4,5));
or
或者
var myObjArr = [new PricingData(1,2,3,4,5), new PricingData(1,2,3,4,5)];
var myObjArr = [new PricingData(1,2,3,4,5), new PricingData(1,2,3,4,5)];