开发者

javascript : how can i get array in different ways?

hi i have a little problem with my javascript can i make the simple way to execute conte开发者_JS百科nt of array with different character of word? for example :

var word = new Array();
word [0] = "is";
word [1] = "am";
.
.
.
.
word [100] = "when";

var word should be access with 3 ways,in order to reduce process to execute arrays..

first : " "+ word +" ";
second : " "+ word;
third : word +" ";

-thank you for helping-


I'm not exactly sure what you are chasing (fill me in and I'll update), but I'd like to point out a far better way of filling in that array literal...

var word = [
   'is',
   'am'
];

You can see the index is calculated automatically, and you are not required to repeat the var name for each member definition.

Update

Maybe you want something you can call and get the next array member each time. This should do it...

function getNextMember(array, startIndex) {
   startIndex = startIndex || 0;

   return function() {
      startIndex++;  
      return array[startIndex];
   };
   
};

var getNextWord = getNextMember(word);

alert(getNextWord() + ' ' + getNextWord());

See it on jsFiddle.

And of course, if you are feeling naughty, you could add that function to Array's prototype.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜