开发者

Is there another way of setting the array values in javascript

Again I'm still new to this javascript thing, so just would like to know if there is another way of setting the values of an array (just like declaring it);

//correct way of declaring an array and reusing
var adata = new Array('1','2','3');
//reusing of variable
adata[0] = '4';
adata[1] = '5';
adata[2] = '6'

This part is my question; I want to declare the values of the开发者_开发问答 array just like declaring them to minimize the number of lines;

//array declaration
var data = new Array('1','2','3');
//reusing of variable
data = ['4','5','6']; ---> (as an example) I get an error msg "Invalid assignment left-hand side"

is this possible? If so, what is the correct syntax? I hope I'm making sense.

Thanking you in advance.


Although I wouldn't call it "reuse", this works for me:

>>> var data = new Array('1', '2', '3');
>>> data = ['4', '5', '6']

The >>>'s come from the terminal (I'm using the Firebug console).

This makes the variable data reference another array. Recall that variables in JS are actually references that kinda "point to" objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜