将数组作为参数传递给对象

时间:2012-07-31 01:06:29

标签: javascript

我是javascript的新手并且可能尝试做一些愚蠢的事情,因为一小时的谷歌搜索没有导致任何例子。

我正在尝试学习对象,所以我创建了一个像这样的构造函数:

//generic object
function clip(name, xposition, firstVisibleFrame, lastVisibleFrame, playStart, playEnd, defaultFrame, seqLoopStart, seqLoopEnd, backStateLevel){
this.name = name;
this.xposition = xposition;
this.outlinesArray =[];
this.firstVisibleFrame = firstVisibleFrame;
this.lastVisibleFrame = lastVisibleFrame;
this.outlinePath = ("outlines/" +this.name +"_");
this.playStart = playStart;
this.playEnd = playEnd;
this.defaultFrame = defaultFrame;
this.seqLoopStart = seqLoopStart;
this.seqLoopEnd = seqLoopEnd;
this.backStateLevel = backStateLevel;
};

接下来,我将使用以下内容创建该对象的新实例:

livingroomTable = new clip("livingroomTable");

...但是现在我真的被卡住了因为我应该传递给函数的下一个参数实际上是一个包含数百个值的巨大数组..

我试着将数组放在参数中,如下所示:

 livingroomTable = new clip("livingroomTable", [value1, value2, value3, value4, value5 and so on and so on]);

但这给了我一个语法错误。

我也不想让这个数组成为全局变量。将值数组作为函数调用参数的一部分传递的正确语法是什么?

编辑:

以下是实际代码:

//generic object
function clip(name, xposition, firstVisibleFrame, lastVisibleFrame, playStart, playEnd, defaultFrame, seqLoopStart, seqLoopEnd, backStateLevel){
this.name = name; //objektin nimi tulee funktion kutsusta
this.xposition = xposition;
this.outlinesArray =[];
this.firstVisibleFrame = firstVisibleFrame;
this.lastVisibleFrame = lastVisibleFrame;
this.outlinePath = ("outlines/" +this.name +"_");
this.playStart = playStart;
this.playEnd = playEnd;
this.defaultFrame = defaultFrame;
this.seqLoopStart = seqLoopStart;
this.seqLoopEnd = seqLoopEnd;
this.backStateLevel = backStateLevel;
};


livingroomTable = new clip(
"livingroomTable", //name

["1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1220.69","1216.69","1207.69","1194.69","1167.69","1157.01","1147.24","1137.62","1127.74","1118.55","1109.88","1100.17","1090.51","1080.69","1070.28","1059.5","1048.87","1038.21","1027.6","1017.31","1007.31","997.208","987.153","977.152","967.848","959.258","950.808","942.887","935.103","927.326","919.256","910.834","902.452","894.231","886.561","879.269","872.072","865.057","858.292","851.484","844.527","837.655","830.747","823.629","815.898","807.221","798.142","789.152","780.341","771.358","762.248","753.089","743.97","734.954","726.148","717.699","709.299","700.826","692.102","682.849","673.258","663.37","653.188","642.48","631.498","620.551","609.866","599.544","589.439","579.088","568.508","557.794","547.028","536.037","524.912","514.043","502.899","491.412","480.102","469.097","457.844","446.368","434.919","423.84","412.74","401.657","390.55","379.284","368.162","357.316","346.606","335.973","325.144","314.01","302.658","291.207","279.392","267.133","254.62","241.775","229.179","216.763","204.423","192.239","180.41","168.484","155.502","141.701","127.828","114.008","100.403","87.0368","73.5946","59.7342","45.3524","31.0484","16.8761","helmi.96","-11.522","-25.6947","-39.5332","-53.1723","-66.7146","-80.253","-93.1294","-106.086","-119.531","-133.534","-148.801","-165.444","-183.67","-202.813","-222.634","-242.769","-263.074","-283.495","-303.787","-322.998","-341.376","-359.338","-377.081","-395.064","-413.628","-432.233","-451.409","-472.675","-493.827","-515.078","-537.029","-558.747","-583.8","-610.595","-645.864","-695.789","-711.965","-737.979","-752.979","-836.048","-862.75","-864.75","-884.835","-911.473","-927.473","-969.103","-1030.36","-1002.83","-1036.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08","-1048.08"];, //xposition

352, //firstVisibleFrame

545, //lastVisibleFrame

2058, //playStart

2304, //playEnd

2175, //defaultFrame

-1000, //seqLoopStart

-1000, //seqLoopEnd

1 //backStateLevel

);
实际上,我认为我的问题是阵列末尾的分号是对的吗? :d

3 个答案:

答案 0 :(得分:1)

您正在使用的原则没有任何问题。

您可以将数组放入变量中,并将其传递给函数:

theArray = [1,2,3,4,5];
livingroomTable = new clip("livingroomTable", theArray);

或者您可以直接在调用中使用数组文字:

livingroomTable = new clip("livingroomTable", [1,2,3,4,5]);

答案 1 :(得分:1)

你得到一个 SyntaxError ,因为你在数组的末尾有一个;分号。

删除它,错误应该消失。

["1220.69","1220.69", ..., "-1048.08","-1048.08","-1048.08"];, // <-- right here

看起来应该是这样......

["1220.69","1220.69", ..., "-1048.08","-1048.08","-1048.08"], // <-- just a comma

您可能需要考虑使用代码验证程序,例如JSHint

答案 2 :(得分:-1)

因此,构造函数不期望字符串和数组参数。你必须这样打电话:

livingroomTable = new clip("livingroomTable", value1, value2, value3, value4, ...);