2012-07-31 107 views
1

我是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 

); 

Hmmmm其實我覺得我的問題是在陣列權的最後的分號? :D

+0

如果你得到一個* SyntaxError *,你需要顯示你的實際代碼來獲得幫助。 – 2012-07-31 01:08:45

+0

好吧,但陣列將是巨大的:D – 2012-07-31 01:09:52

+0

http://www.jslint.com/可能會幫助 – valentinas 2012-07-31 01:10:13

回答

1

你得到a 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

+0

是的,你說得對!多麼令人尷尬的錯誤:S – 2012-07-31 01:23:29

-1

所以,你的構造函數不要求字符串和數組參數。你必須這樣打電話:

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

你正在使用的原理沒有錯。

你可以把數組中的一個變量,並傳遞給函數:

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

或者您也可以在通話直接使用字面數組:

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

感謝您的保證!我注意到我的語法錯誤是由分號(以及更早和更多的逗號)引起的:S對不起! – 2012-07-31 01:18:53

相關問題