简体   繁体   English

非常简单的javascript数组语法

[英]very simple, javascript arrays syntax

I have a very simple JS Arrays question, my simple canvas game has been behaving differently when I replaced one block of code with another. 我有一个非常简单的JS Arrays问题,当我用一个代码块替换另一个代码块时,我的简单画布游戏表现不同。 Could you look them over and see why they are functionally different from one another, and maybe provide a suggestion? 你能看一下它们,看看为什么它们在功能上彼此不同,并且可能提供一个建议吗? I may need these arrays to have 20+ items so I'm looking for a more condensed style. 我可能需要这些数组有20多个项目,所以我正在寻找更浓缩的风格。

There's this one, which is short enough for me to work with, but doesn't run well: 有这个,这个对我来说足够短,但运行不顺利:

var srd=new Array(1,1,1);
var sw=new Array(0,0,0);
var sang=new Array(0,0,0);
var sHealth=new Array(20,20,20);

And then there's the original one, which is longer but works fine: 然后是原始的,但更长但工作正常:

var srd = new Array();
srd[1] = 1; 
srd[2] = 1; 
srd[3] = 1; 
var sw = new Array();
sw[1] =0; 
sw[2] =0; 
sw[3] =0; 
var sang = new Array();
sang[1] = 0; 
sang[2] = 0; 
sang[3] = 0;
var sHealth = new Array();
sHealth[1] = 20; 
sHealth[2] = 20; 
sHealth[3] = 20;

Arrays are zero-indexed in JavaScript. 数组在JavaScript中为零索引。 The first element is 0 , not 1 : 第一个元素是0 ,而不是1

var srd = new Array();
srd[0] = 1; 
srd[1] = 1; 
srd[2] = 1; 

Also, you may want to use the more common array constructor: 此外,您可能希望使用更常见的数组构造函数:

var srd = [1, 1, 1];

I have a feeling that you may be assuming that the first element is 1 instead of 0 , which is why the first version doesn't work while the second one does. 我有一种感觉,你可能假设第一个元素是1而不是0 ,这就是为什么第一个版本不起作用而第二个版本不起作用的原因。

它看起来像是在索引0处启动数组而另一个从索引1开始

It depends on your implementation, but it's likely because of arrays being 0-indexed. 这取决于您的实现,但可能是因为数组被索引为0。 In your first block of code, each number is offset by one index spot from the second block. 在第一个代码块中,每个数字都被第二个块中的一个索引点偏移。 The first one is equivalent to: 第一个相当于:

var srd = new Array();
srd[0] = 1; 
srd[1] = 1; 
srd[2] = 1; 

in the way you wrote it for the second block. 在你为第二个块写它的方式。

You should do this....in Arrays values are stored as such that first one is at 0 and so on. 你应该这样做....在数组中,值存储为第一个值为0,依此类推。 so 1st value will be accessed as this... 所以第一个值将被访问为......

 var x = abc[0]; //first value of array abc being assigned to x.

Do this (you see i actually read your question and this is what you like) 这样做 (你看我真的读过你的问题,这就是你喜欢的)

 var srd=['',1,1,1];
 var sw=['',0,0,0];
 var sang=['',0,0,0];
 var sHealth=['',20,20,20];

you can declare an array(object) in javascript like this 你可以像这样在javascript中声明一个数组(对象)

var x = []; var x = []; -------------Literal - its a shortcut provided by JS to quickly declare something as an Array. ------------- Literal - 它是JS提供的快捷方式,可以快速声明一些数组。

var x = new Array; var x = new Array; --Array constructor --Array 构造函数

Things to look up regarding 要查看的事情

literal 文字

  • object literal 对象文字
  • proto
  • word new 新词
  • function object 功能对象
  • function property prototype 功能属性原型

You can also do these: 你也可以这样做:

var x=1,y=2, z=3, f;

var b = something || {}; \\\\become a copy of object called something if it's not there then become an empty object.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM